力扣第一题(两数之和)
class Solution {
public int[] twoSum(int[] nums
, int target
) {
HashMap
<Integer, Integer> hashMap
= new HashMap<>();
for (int i
= 0; i
< nums
.length
; i
++) {
int tmp
= target
- nums
[i
];
if (hashMap
.containsKey(tmp
)){
int[] result
={i
,hashMap
.get(tmp
)};
return result
;
}
hashMap
.put(nums
[i
],i
);
}
return null
;
}
}
转载请注明原文地址: https://lol.8miu.com/read-12528.html