Two Sum
给定一个整数数组,和一个目标和,找到数组中的两个数,使其相加等于目标和
注意:返回值是两个数在数组中的下标(基于1),且index1小于index2。
例如:
输入: numbers={2, 7, 11, 15}, target=9
输出: index1=1, index2=2
代码(java):
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res =new int[]{-1,-1};
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int i=0;i<nums.length;i++){
map.put(target-nums[i],i);
}
for (int i=0;i<nums.length;i++){
if (map.containsKey(nums[i]) ){
int index = map.get(nums[i]);
if (index > i){
res[0] = i+1;
res[1] = index+1;
return res;
}
else if (index < i){
res[0] = index+1;
res[1] = i+1;
return res;
}
}
}
return res;
}
}
代码(javascript):
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
//存储key:number,value:index
var map = {};
var res = [-1,-1];
for (var i = 0; i < nums.length; i++) {
var item = nums[i];
if ( (target - item) in map){
res[0] = map[target - item] + 1;
res[1] = i + 1;
//找到即返回
return res;
}
//数组元素存储在map中
else{
map[item] = i;
}
}
return res;
};