class Solution {
//Time:O(N) Space:O(N)
public int[] twoSum(int[] nums, int target) {
//key-> target - number value->index
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
int key = target - nums[i];
if(map.containsKey(key)){
return new int[]{map.get(key), i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("Not two such number!");
}
}
1.两数之和
于 2018-08-15 01:21:54 首次发布