diff --git a/notes/Leetcode 题解 - 哈希表.md b/notes/Leetcode 题解 - 哈希表.md index 9e0f8f90..34102a6b 100644 --- a/notes/Leetcode 题解 - 哈希表.md +++ b/notes/Leetcode 题解 - 哈希表.md @@ -14,14 +14,14 @@ - Java 中的 HashSet 用于存储一个集合,可以查找元素是否在集合中。如果元素有穷,并且范围不大,那么可以用一个布尔数组来存储一个元素是否存在。例如对于只有小写字符的元素,就可以用一个长度为 26 的布尔数组来存储一个字符集合,使得空间复杂度降低为 O(1)。 -- Java 中的 HashMap 主要用于映射关系,从而把两个元素联系起来。HashMap 也可以用来对元素进行计数统计,此时键为元素,值为计数。和 HashSet 类似,如果元素有穷并且范围不大,可以用整型数组来进行统计。在对一个内容进行压缩或者其它转换时,利用 HashMap 可以把原始内容和转换后的内容联系起来。例如在一个简化 url 的系统中(535\. Encode and Decode TinyURL (Medium),[Leetcode](https://leetcode.com/problems/encode-and-decode-tinyurl/description/) / [力扣](https://leetcode-cn.com/problems/encode-and-decode-tinyurl/description/)),利用 HashMap 就可以存储精简后的 url 到原始 url 的映射,使得不仅可以显示简化的 url,也可以根据简化的 url 得到原始 url 从而定位到正确的资源。 +- Java 中的 HashMap 主要用于映射关系,从而把两个元素联系起来。HashMap 也可以用来对元素进行计数统计,此时键为元素,值为计数。和 HashSet 类似,如果元素有穷并且范围不大,可以用整型数组来进行统计。在对一个内容进行压缩或者其它转换时,利用 HashMap 可以把原始内容和转换后的内容联系起来。例如在一个简化 url 的系统中(535\. Encode and Decode TinyURL (Medium),[Leetcode](https://leetcode.com/problems/encode-and-decode-tinyurl/description/) / [力扣](https://leetcode.cn/problems/encode-and-decode-tinyurl/description/)),利用 HashMap 就可以存储精简后的 url 到原始 url 的映射,使得不仅可以显示简化的 url,也可以根据简化的 url 得到原始 url 从而定位到正确的资源。 ## 1. 数组中两个数的和为给定值 1\. Two Sum (Easy) -[Leetcode](https://leetcode.com/problems/two-sum/description/) / [力扣](https://leetcode-cn.com/problems/two-sum/description/) +[Leetcode](https://leetcode.com/problems/two-sum/description/) / [力扣](https://leetcode.cn/problems/two-sum/description/) 可以先对数组进行排序,然后使用双指针方法或者二分查找方法,时间复杂度为 O(NlogN)。需要注意的是,题目要求返回原数组的下标,排序会打乱下标,因此要把元素和原下标一起排序,空间复杂度为 O(N)。 @@ -45,7 +45,7 @@ public int[] twoSum(int[] nums, int target) { 217\. Contains Duplicate (Easy) -[Leetcode](https://leetcode.com/problems/contains-duplicate/description/) / [力扣](https://leetcode-cn.com/problems/contains-duplicate/description/) +[Leetcode](https://leetcode.com/problems/contains-duplicate/description/) / [力扣](https://leetcode.cn/problems/contains-duplicate/description/) ```java public boolean containsDuplicate(int[] nums) { @@ -61,7 +61,7 @@ public boolean containsDuplicate(int[] nums) { 594\. Longest Harmonious Subsequence (Easy) -[Leetcode](https://leetcode.com/problems/longest-harmonious-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-harmonious-subsequence/description/) +[Leetcode](https://leetcode.com/problems/longest-harmonious-subsequence/description/) / [力扣](https://leetcode.cn/problems/longest-harmonious-subsequence/description/) ```text Input: [1,3,2,2,5,2,3,7] @@ -91,7 +91,7 @@ public int findLHS(int[] nums) { 128\. Longest Consecutive Sequence (Medium) -[Leetcode](https://leetcode.com/problems/longest-consecutive-sequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-consecutive-sequence/description/) +[Leetcode](https://leetcode.com/problems/longest-consecutive-sequence/description/) / [力扣](https://leetcode.cn/problems/longest-consecutive-sequence/description/) ```text Given [100, 4, 200, 1, 3, 2],