码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 001. Two Sum

时间:2015-04-21 12:45:40      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2


主要有两种思路,一种是暴力破解,循环寻找答案,算法复杂度为O(n2),空间复杂度为O(1);另一种思路是先排序再查找,不同排序查找方法可能会有不同结果。

暴力破解的代码很简单,提交上去直接超时了。需要考虑各种边界以及不存在结果的情况,后面改进

代码一:

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

第二种排序可以采用双指针的方式

代码二:

 1 public class Solution {
 2 
 3     static class Pair implements Comparable<Pair> {
 4         int value, index;
 5         public Pair(int v, int id) {
 6             value = v;
 7             index = id;
 8         }
 9 
10         @Override
11         public int compareTo(Pair b) {
12             return this.value - b.value;
13         }
14     }
15 
16     public static int[] twoSum(int[] numbers, int target) {
17         int[] res = new int[2];
18         Pair[] pairs = new Pair[numbers.length];
19 
20         // get pairs and sort
21         for (int i = 0; i < numbers.length; ++i) {
22             pairs[i] = new Pair(numbers[i], i + 1);
23         }
24         Arrays.sort(pairs);
25 
26         // two points
27         int left = 0, right = numbers.length - 1, sum = 0;
28         while (left < right) {
29             sum = pairs[left].value + pairs[right].value;
30             if (sum == target) {
31                 res[0] = pairs[left].index;
32                 res[1] = pairs[right].index;
33                 if (res[0] > res[1]) {
34                     // swap them
35                     res[0] ^= res[1];
36                     res[1] ^= res[0];
37                     res[0] ^= res[1];
38                 }
39                 break;
40             } else if (sum > target) {
41                 --right;
42             } else {
43                 ++left;
44             }
45         }
46 
47         return res;
48     }
49 }

也可以采用哈希表,

代码三:

 1 public class Solution {
 2     public static int[] twoSum(int[] numbers, int target) {
 3         int[] res = new int[2];
 4         HashMap<Integer, Integer> nums = new HashMap<Integer, Integer>();
 5 
 6         for (int i = 0; i < numbers.length; ++i) {
 7             // add i-th number
 8             Integer a = nums.get(numbers[i]);
 9             if (a == null)
10                 nums.put(numbers[i], i);
11 
12             // find (target - numbers[i])
13             a = nums.get(target - numbers[i]);
14             if (a != null && a < i) {
15                 res[0] = a + 1;
16                 res[1] = i + 1;
17                 break;
18             }
19         }
20         return res;
21     }
22 }

时间复杂度为O(n),空间复杂度为O(n)

 

[LeetCode] 001. Two Sum

标签:

原文地址:http://www.cnblogs.com/myshuangwaiwai/p/4443966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!