https://leetcode.com/problems/two-sum/description/?envType=study-plan-v2&envId=top-interview-150
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) return new int[]{i, j};
}
}
return new int[]{};
}
}
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(nums[i], i);
for (int i = 0; i < nums.length; i++) {
int tmp = target - nums[i];
if (map.containsKey(tmp) && map.get(tmp) != i) return new int[]{i, map.get(tmp)};
}
return new int[]{};
}
}
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
int[] ans = new int[2];
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(target-nums[i])) {
ans[0] = map.get(target-nums[i]);
ans[1] = i;
return ans;
}
map.put(nums[i],i);
}
return ans;
}
}