一、题目
给定一个整数数组,返回两个数字的索引,使它们相加成为一个特定的目标。
您可能假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素。
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
二、解法概览
解法1:暴力法
在leetcode中运行用时: 65 ms, 在Two Sum的Java提交中击败了19.51% 的用户
复杂度分析:
时间复杂度:O(n^2)
空间复杂度:O(1)。
解法思路:按大神的推荐,任何一道算法题,都有对应的暴力解法。当实在想不出其他方法时,暴力法就是你的选择。
public int[] twoSum1(int[] nums, int target) {
int length = nums.length;
for (int i = 0; i < length; ++ i)
for (int j = i + 1; j < length; ++ j)
if (nums[i] + nums[j] == target)
return new int[]{i,j};
return null;
}
点评:上面办法很多人都能想到,大学时候最擅长的算法(for循环嵌套for循环),但是这种算法自然性能也不是最好的,想知道更好办法,请看解法2
解法2:两遍哈希表
在leetcode中执行用时: 11 ms, 在Two Sum的Java提交中击败了74.59% 的用户
复杂度分析:
时间复杂度:O(n)
空间复杂度:O(n)。
解题思路:以空间换时间
public int[] twoSum2(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
点评:解法2采用了hash表查询方法,hash查找是一种高效的查找,思路是先把所有数值当key存入hash表,然后逐一检查跟当前数值不一样的满足条件的数值,但是这里做了一些无用功,你看解法3就知道无用功在哪里
解法3:一遍哈希表
在leetcode中执行用时: 4 ms, 在Two Sum的Java提交中击败了99.61% 的用户
复杂度分析:
时间复杂度:O(n)
空间复杂度:O(n)。
解题思路:所求的两个值,一旦有一个已经在哈希表中,那么另一个值便可在数组遍历过程中找出
public int[] twoSum3(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int[] res = new int[2];
for (int i = 0; i < nums.length; ++i) {
if (m.containsKey(target - nums[i])) {
res[0] = i;
res[1] = m.get(target - nums[i]);
break;
}
m.put(nums[i], i);
}
return res;
}
点评:解法3对解法2的hash查找方法做了优化,减少了一些无用的操作,思路就是先查找找不到就存入hash表,因为如果某个数值不存在,那么当第一次找不到时候,在后面找到他的另一对数值时候也能再次找到,所以不需要解法2那样先全部存一遍然后再判断过滤,不得不承认这个方法更加高明简洁。
完整代码如下:
package com.xu.leetcode;
import java.util.HashMap;
import java.util.Map;
public class TwoSumTest {
public static void main(String[] args) {
int[] nums = {1, 3, 5, 7, 10, 13, 15, 20};
int target = 20;
long a= System.nanoTime();//获取当前系统时间(纳秒)
TwoSumTest.twoSum1(nums, target);
System.out.print("程序执行时间为:");
System.out.println(System.nanoTime()-a+"纳秒");
long b= System.nanoTime();//获取当前系统时间(纳秒)
TwoSumTest.twoSum2(nums, target);
System.out.print("程序执行时间为:");
System.out.println(System.nanoTime()-b+"纳秒");
long c= System.nanoTime();//获取当前系统时间(纳秒)
TwoSumTest.twoSum3(nums, target);
System.out.print("程序执行时间为:");
System.out.println(System.nanoTime()-c+"纳秒");
}
public static void twoSum1(int[] nums, int target) {
int length = nums.length;
for (int i = 0; i < length; ++i)
for (int j = i + 1; j < length; ++j)
if (nums[i] + nums[j] == target)
System.out.println("i:"+i+",j:"+j);
}
public static void twoSum2(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {//这里假如是排序的数组
int complement = target - nums[i];
//这里可能来回找了两遍,怎么优化呢??
if (map.containsKey(complement) && map.get(complement) != i) {
System.out.println("i:"+i+",j:"+map.get(complement));
}
}
}
public static void twoSum3(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int[] res = new int[2];
for (int i = 0; i < nums.length; ++i) {
if (m.containsKey(target - nums[i])) {
res[0] = i;
res[1] = m.get(target - nums[i]);
System.out.println("i:"+res[0]+",j:"+res[1]);
}else{
m.put(nums[i], i);
}
}
}
}
结果截图