classTwoSum{ Map<Integer, Integer> freq = new HashMap<>();
/** Initialize your data structure here. */ publicTwoSum(){ this.freq = new HashMap<>();
} /** Add the number to an internal data structure.. */ publicvoidadd(int number){ freq.put(number, freq.getOrDefault(number, 0) + 1); } /** Find if there exists any pair of numbers which sum is equal to the value. */ publicbooleanfind(int value){ for (int i: freq.keySet()){ int other = value - i; if (other != i && freq.containsKey(other)) returntrue; elseif (other == i && freq.get(other) > 1) returntrue; } returnfalse; } }
频繁使用add()(然后超出力扣的时间限制了):
sum 中储存了所有加入数字可能组成的和,每次 find 只要花费 O(1) 的时间在集合中判断一下是否存在就行了,显然非常适合频繁使用 find 的场景,但不适合频繁使用add的场景。
classTwoSum{ Set<Integer> sum = new HashSet<>(); List<Integer> nums = new LinkedList<>();
/** Initialize your data structure here. */ publicTwoSum(){ this.sum = new HashSet<>(); this.nums = new LinkedList<>();
} /** Add the number to an internal data structure.. */ publicvoidadd(int number){ for (int i: nums){ sum.add(number+i); } nums.add(number); } /** Find if there exists any pair of numbers which sum is equal to the value. */ publicbooleanfind(int value){ return sum.contains(value); } }
classSolution{ public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums);
return nSumTarget(nums, 4, 0, target); }
List<List<Integer>> nSumTarget(int[] nums, int n, int start, int target){ int sz = nums.length; List<List<Integer>> res = new ArrayList<>(); if (n < 2 || sz < n) return res;
if (n == 2){ //2Sum base case int left = start, right = sz - 1; while (left < right){ int sum = nums[left] + nums[right]; int left_init = nums[left], right_init = nums[right];
if (sum == target){ List<Integer> a = new ArrayList<>(); a.add(left_init); a.add(right_init); res.add(a); while (left < right && nums[left] == left_init) left++; while (left < right && nums[right] == right_init) right--; } elseif (sum < target){ while (left < right && nums[left] == left_init) left++; } elsewhile (left < right && nums[right] == right_init) right--; } } else{ //递归计算(n-1)Sum结果 for (int i = start; i < sz; i++){ int num = nums[i];
List<List<Integer>> res_temp = nSumTarget(nums, n - 1, i + 1, target - num);
for (List list : res_temp){ list.add(num); //(n-1)Sum加上num[i]就是nSum res.add(list); }
while (i < sz - 1 && num == nums[i + 1]) i++; } } return res; } }
classSolution{ publicbooleancontainsDuplicate(int[] nums){ HashMap<Integer, Integer> value = new HashMap<>(); for (int i: nums){ if (value.containsKey(i)) returntrue; else value.put(i,1); }
returnfalse; } }
哈希表(用getOrDefault()方法):
1 2 3 4 5 6 7 8 9 10 11 12
classSolution{ publicbooleancontainsDuplicate(int[] nums){ HashMap<Integer, Integer> value = new HashMap<>(); for (int i: nums){ value.put(i,value.getOrDefault(i, 0) + 1); if (value.get(i) > 1) returntrue; }
returnfalse; } }
哈希集合(遍历后用集合长度与数组长度比对的方法):
1 2 3 4 5 6 7 8 9 10
classSolution{ publicbooleancontainsDuplicate(int[] nums){ Set<Integer> value = new HashSet<>(); for (int i: nums){ value.add(i); }
return value.size() < nums.length; } }
哈希集合(用contains()方法):
1 2 3 4 5 6 7 8 9 10
classSolution{ publicbooleancontainsDuplicate(int[] nums){ Set<Integer> value = new HashSet<>(); for (int i: nums){ if (value.contains(i)) returntrue; value.add(i); } returnfalse; } }
int max = 0, pre = sortedValues.get(0), count = 1; for (int num: sortedValues){ if (pre + 1 == num){ count++; } else{ max = Math.max(max, count); count = 1; } pre = num; } max = Math.max(max, count);