public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; i++){
hm.put(nums[i], i+1);
}
int[] index = new int[2];
int j =1;
for(int k =0; k < nums.length; k++){
if(hm.containsKey(target - nums[k]) && ((k+1) < hm.get(target - nums[k]))){
index[0] = k+1;
index[1] = hm.get(target - nums[k]);
}
}
return index;
}
}
leetcode--two sum
最新推荐文章于 2022-11-09 11:23:01 发布