mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-30 18:23:52 +00:00
Update Leetcode 题解 - 哈希表.md
代码经力扣判题验证;整理 535 题的链接,难度标注按力扣更新,示例代码块的语言标注改为 text。
This commit is contained in:
@@ -13,9 +13,7 @@
|
||||
|
||||
- Java 中的 **HashSet** 用于存储一个集合,可以查找元素是否在集合中。如果元素有穷,并且范围不大,那么可以用一个布尔数组来存储一个元素是否存在。例如对于只有小写字符的元素,就可以用一个长度为 26 的布尔数组来存储一个字符集合,使得空间复杂度降低为 O(1)。
|
||||
|
||||
- Java 中的 **HashMap** 主要用于映射关系,从而把两个元素联系起来。HashMap 也可以用来对元素进行计数统计,此时键为元素,值为计数。和 HashSet 类似,如果元素有穷并且范围不大,可以用整型数组来进行统计。在对一个内容进行压缩或者其它转换时,利用 HashMap 可以把原始内容和转换后的内容联系起来。例如在一个简化 url 的系统中 [Leetcdoe : 535. Encode and Decode TinyURL (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/encode-and-decode-tinyurl/description/),利用 HashMap 就可以存储精简后的 url 到原始 url 的映射,使得不仅可以显示简化的 url,也可以根据简化的 url 得到原始 url 从而定位到正确的资源�) / [力扣](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.com/problems/encode-and-decode-tinyurl/description/)),利用 HashMap 就可以存储精简后的 url 到原始 url 的映射,使得不仅可以显示简化的 url,也可以根据简化的 url 得到原始 url 从而定位到正确的资源。
|
||||
|
||||
|
||||
## 1. 数组中两个数的和为给定值
|
||||
@@ -64,7 +62,7 @@ public boolean containsDuplicate(int[] nums) {
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/longest-harmonious-subsequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-harmonious-subsequence/description/)
|
||||
|
||||
```html
|
||||
```text
|
||||
Input: [1,3,2,2,5,2,3,7]
|
||||
Output: 5
|
||||
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
|
||||
@@ -90,11 +88,11 @@ public int findLHS(int[] nums) {
|
||||
|
||||
## 4. 最长连续序列
|
||||
|
||||
128\. Longest Consecutive Sequence (Hard)
|
||||
128\. Longest Consecutive Sequence (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/longest-consecutive-sequence/description/) / [力扣](https://leetcode-cn.com/problems/longest-consecutive-sequence/description/)
|
||||
|
||||
```html
|
||||
```text
|
||||
Given [100, 4, 200, 1, 3, 2],
|
||||
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user