您的位置:首页 > 其它

leetcode-1 Two Sum 找到数组中两数字和为指定和

2015-03-12 16:02 357 查看


问题描述:在一个数组(无序)中快速找出两个数字,使得两个数字之和等于一个给定的值。假设数组中肯定存在至少一组满足要求。

《剑指Offer》P214(有序数组) 《编程之美》P176

Que:Given an array of integers, find twonumbers such that they add up to a specific target number.

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

You may assume that each input would haveexactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

 

一、暴力穷举法 时间复杂度:O(N^2)

代码:

// 暴力解法,时间复杂度(O(n^2)),性能太差无法通过
public int[] twoSum1(int[] numbers, int target) {
if (numbers != null) {
int i, j = 0;
for (i = 0; i < numbers.length; i++) {
for (j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] == target) {
int[] result = { ++i, ++j };
return result;
}
}
}
}
return null;
}


二、Hash表法,以空间换时间:时间复杂度:O(N)空间复杂度O(N)
更快的查找方法:hash表,给定一个数字,根据hash映射查找另一个数字是否在数组中,只需O(1)的时间,但需要承担O(N)的hash表存储空间。
C++可以使用STL中的hash_map,java使用HashMap
代码:

// 使用HashMap(查找的时间复杂度为O(1))
// 由题目假设知只有一对数满足该情况,故每个数都是唯一的,不存在重数的情况
public int[] twoSum2(int[] numbers, int target) {
if (numbers != null) {
// 因为Hashmap仅提供通过key获得value,故
// HashMap value放置与numers[index]匹配的数值,key放置index;,故
// 在下面循环时每一次查询map中的value是否有相等的值,有即相互匹配
// 其思想在于用index的value表示数组中的该数据,map中的key与之匹配,并在数组中寻找匹配值
HashMap<Integer, Integer> num_map = new HashMap<>();
for (int i = 0; i < numbers.length; i++) {
if (num_map.containsKey(numbers[i])) {
int index = num_map.get(numbers[i]);
int[] result = { ++index, ++i };
return result;
} else {
num_map.put(target - numbers[i], i);
}
}
}
return null;
}


三、很容易可以想到先将查找的数组排序,然后用二分查找等方法进行查找。本题中可以直接对两个数字的和进行一个有序的遍历(当然和不用全部真正计算出来)。

1.首先对数组进行排序,时间复杂度为O(NlogN)

2.然后从i=0,j=end开始和末位的两个数字开始,计算两个之和sum,若sum大于目标值target,则需要一个较小的因子,j--;反之,i++;直至找到最终的结果。

代码:(这里没有返回相应原始位置,只是返回了两个对应数据)

public int[] twoSum3(int[] numbers, int target) {
if (numbers != null) {
// 先进行排序,这里使用归并排序
new Merge_Sort().Merge_Sort(numbers, new int[numbers.length], 0,
numbers.length - 1);
// 实现该查找算法
int ahead = numbers.length - 1;
int behind = 0;
while (ahead > behind) {
// 注意result和要考虑两个较大int相加溢出的问题
long result = numbers[ahead] + numbers[behind];

if (result == target) {
int[] sum = { numbers[behind] , numbers[ahead] };
//如果要返回两个原始位置值,是否意味着还是重新进行两次查询;
return sum;
}

if (result < target) {
behind++;
} else {
ahead--;
}
}
}

return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐