大神版:使用哈希表,算法复杂度o(n);
空间复杂度O(n);

class Solution {
public int[] twoSum(int[] nums, int target) {
if(nums == null) return null;
int[] retval = new int[2];
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(nums[i])) {
retval[0] = map.get(nums[i]);
retval[1] = i;
break;
}
else {
map.put(target-nums[i], i);
}
}
return retval;
}
}

菜鸟:me

算法复杂度O(n^2)
空间复杂度O (1);

public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}