diff --git a/notes/Leetcode 题解 - 哈希表.md b/notes/Leetcode 题解 - 哈希表.md index 66f59b16..5448d043 100644 --- a/notes/Leetcode 题解 - 哈希表.md +++ b/notes/Leetcode 题解 - 哈希表.md @@ -24,7 +24,7 @@ 可以先对数组进行排序,然后使用双指针方法或者二分查找方法。这样做的时间复杂度为 O(NlogN),空间复杂度为 O(1)。 用 HashMap 存储数组元素和索引的映射,在访问到 nums[i] 时,判断 HashMap 中是否存在 target - nums[i],如果存在说明 target - nums[i] 所在的索引和 i 就是要找的两个数。该方法的时间复杂度为 O(N),空间复杂度为 O(N),使用空间来换取时间。 - + ```java public int[] twoSum(int[] nums, int target) { HashMap indexForNum = new HashMap<>(); @@ -45,13 +45,19 @@ public int[] twoSum(int[] nums, int target) { [Leetcode](https://leetcode.com/problems/contains-duplicate/description/) / [力扣](https://leetcode-cn.com/problems/contains-duplicate/description/) + ```java public boolean containsDuplicate(int[] nums) { Set set = new HashSet<>(); for (int num : nums) { set.add(num); + //if set.containsKey(num){ + // return true; + //} + //set.add(num); } return set.size() < nums.length; + //return false; } ```