剑指offer简单题汇总

 
1.两个数之和
给出一个整数数组,请在数组中找出两个加起来等于目标值的数,
你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index2.。注意:下标是从1开始的
假设给出的数组中只存在唯一解
例如:

给出的数组为 {2, 7, 11, 15},目标值为9
输出 ndex1=1, index2=2

 1 public class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         
 4         int index1 = 0,index2 =0;
 5         int sum = target;
 6         
 7         for(int i = 0; i < nums.length; i++){
 8             for(int j = i+1; j < nums.length; j++){
 9                     if (sum == nums[i]+ nums[j]){
10                         index1 = i;
11                         index2 = j;
12                     }
13             }
14         }
15         int [] b ={index1+1,index2+1};
16         return b;
17     }
18 }

2.反转整数

将给出的整数x翻转。
例1:x=123,返回321
例2:x=-123,返回-321

你有思考过下面的这些问题么?

如果整数的最后一位是0,那么输出应该是什么?比如10,100
你注意到翻转后的整数可能溢出吗?假设输入是32位整数,则将翻转10000000003就会溢出,你该怎么处理这样的样例?抛出异常?这样做很好,但是如果不允许抛出异常呢?这样的话你必须重新设计函数(比如添加一个额外的参数)。

 1 public class Solution {
 2        public int reverse(int x) {
 3         if(x == 0){
 4             return 0;
 5         }else if(x < 0){
 6             String str = String.valueOf(x);
 7             StringBuffer bf = new StringBuffer(str.substring(1,str.length()));
 8             String s = new String(bf.reverse());
 9             return Integer.parseInt("-"+s);
10         }else {
11             StringBuffer bf = new StringBuffer(String.valueOf(x));
12             String s = new String(bf.reverse());
13             return Integer.parseInt(s);
14         }
15     }
16 }

14.编写一个函数来查找字符串数组中的最长公共前缀。

 1 public class Solution {
 2     //水平
 3     public String longestCommonPrefix(String[] strs) {
 4         if(strs == null || strs.length == 0){
 5             return "";
 6         }
 7         int max = strs[0].length() -1;
 8         for(int i = 1; i < strs.length; i++){
 9             int s = -1;
10             while(s < max && s < strs[i].length()-1){
11                 if(strs[0].charAt(s+1) == strs[i].charAt(s+1)){
12                     s++;
13                 }else{
14                     break;
15                 }
16             }
17             if(s == -1){
18                 return "";
19             }
20             max = s;
21         }
22         return strs[0].substring(0,max+1);
23     }
24 }

20.给出一个仅包含字符'(',')','{','}','['和']',的字符串,判断给出的字符串是否是合法的括号序列
括号必须以正确的顺序关闭,"()"和"()[]{}"都是合法的括号序列,但"(]"和"([)]"不合法。

 1 import java.util.*;
 2 public class Solution {
 3     public boolean isValid(String s) {
 4         if(s == null || s.length() == 0){
 5             return false;
 6         }
 7         Stack<Character> stack = new Stack<>();
 8         char [] c = s.toCharArray();
 9         for(int i = 0; i < s.length(); i++){
10             if(c[i] == '('){
11                 stack.push(')');
12             }else if(c[i] == '{'){
13                  stack.push('}');
14             }else if(c[i] == '['){
15                  stack.push(']');
16             }else if(stack.empty() || c[i] != stack.pop()){
17                 return false;
18             }
19         }
20         return stack.empty();
21     }
22 }

21.将两个有序的链表合并为一个新链表,要求新的链表是通过拼接两个链表的节点来生成的。

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
14         ListNode heap = new ListNode(0);
15         ListNode p = heap;
16         while(l1 != null && l2 != null){
17             if(l1.val <= l2.val){
18                 p.next = l1;
19                 l1 = l1.next;
20             }else{
21                 p.next = l2;
22                 l2 = l2.next;
23             }
24             p = p.next;
25         }
26         if(l1 !=null){
27             p.next = l1;
28         }
29         if(l2 != null){
30             p.next = l2;
31         }
32         return heap.next;
33     }
34 }

猜你喜欢

转载自www.cnblogs.com/linliquan/p/11699166.html