Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

1. Two Sum #163

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

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

/ public class Solution { public int[] twoSum(int[] nums, int target) { if(nums.length < 2) return null; int[] res = new int[2]; for(int i = 0; i < nums.length; i++) { for(int j = 1; j <nums.length; j++) { if(nums[i] == target - nums[j] && i != j) { res[0] = i; res[1] = j; } } } return res; } } /