leecode - 两数之和
题目:
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
初始实现代码:
class Solution {
public int[] twoSum(int[] nums, int target) {
int subcor[] = new int[2];
for (int i = 0 ;i<nums.length ;i++){
for (int j = i+1 ; j< nums.length;j++){
if(nums[i] + nums[j] == target){
subcor[0] = i;
subcor[1] = j;
break;
}
}
}
return subcor;
}
}
简单分析:
- 时间复杂度 O(n^2),空间复杂度O(1),结果是内存使用较高,效率低;
- 当没有符合条件的结果时,无返回信息
再次实现代码
使用一遍哈希表完成题目要求,时间复杂度O(n),空间复杂度O(n)
class Solution {
public int[] twoSum(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");
}
}