class Solution {
public static void main(String[] args){
int[] nums = new int[]{2,7,11,15};
Solution sl = new Solution();
int[] result = sl.twoSum(nums,18);
}
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i< nums.length; i++) {
if(map.containsKey(target - nums[i])) {
return new int[] {map.get(target-nums[i]),i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
leecode-2 整数相加
最新推荐文章于 2024-07-08 22:12:26 发布