class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int[] res = new int[2];
for (int i = 0; i < nums.length; ++i) {
if (m.containsKey(target - nums[i])) {
res[0] = m.get(target - nums[i]);
res[1] = i;
break;
}
m.put(nums[i], i);
}
return res;
}
}
1. class Solution {
2. public int[] twoSum(int[] nums, int target) {
3. HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
4. int[] res = new int[2];
5. for (int i = 0; i < nums.length; ++i) {
6. if (m.containsKey(target - nums[i])) {
7. res[0] = m.get(target - nums[i]);
8. res[1] = i;
9. break;
10. }
11. m.put(nums[i], i);
12. }
13. return res;
14. }
15.}
原文地址:https://www.cnblogs.com/codeleader/p/11634139.html