Update Leetcode 题解 - 哈希表.md

This commit is contained in:
Jie Chen 2020-02-03 16:14:40 +08:00 committed by GitHub
parent f84b140418
commit 4a56358678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<Integer, Integer> 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<Integer> 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;
}
```