两数之和
解法利用HashMap(map.get(Key))实现数的存储和输出
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
int[] ints = new int[2];
for (int i = 0; i < nums.length; i++) {
int goal_num=target-nums[i];
if (map.containsKey(goal_num)){
ints[0]=i;
ints[1]=map.get(goal_num);
break;
}
map.put(nums[i],i);
}
return ints;
}
}
标签:map,goal,nums,int,num,ints,test,目录
From: https://www.cnblogs.com/szwsama/p/17066130.html