自己做的超时,hashmap用的不对,不够简便,学习下别人怎么用的
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
map.put(i, nums[i]);
}
int[] ints = new int[2];
out:
for (Integer o : map.keySet()) {
int j = map.get(o);
if (map.containsValue((target - j))) {
for (Integer integer : map.keySet()) {
if (integer != o && map.get(integer) == (target - j)) {
ints[0] = o;
ints[1] = integer;
break out;
}
}
}
}
return ints;
}
}