Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b404f78

Browse files
✨feat: Add 414
1 parent 6d4cff9 commit b404f78

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

‎Index/排序.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [41. 缺失的第一个正数](https://leetcode-cn.com/problems/first-missing-positive/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/first-missing-positive/solution/yan-ge-on-de-tong-pai-xu-si-lu-yi-ji-wei-wm8d/) | 困难 | 🤩🤩🤩 |
44
| [220. 存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contains-duplicate-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-dlnv/) | 中等 | 🤩🤩🤩 |
5+
| [414. 第三大的数](https://leetcode-cn.com/problems/third-maximum-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/third-maximum-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-pmln/) | 中等 | 🤩🤩🤩🤩 |
56
| [448. 找到所有数组中消失的数字](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/solution/li-yong-tong-pai-xu-de-si-lu-ni-huan-ke-e3t4w/) | 简单 | 🤩🤩🤩 |
67
| [524. 通过删除字母匹配到字典里最长单词](https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/solution/gong-shui-san-xie-xiang-jie-pai-xu-shuan-qi20/) | 中等 | 🤩🤩🤩🤩 |
78
| [581. 最短无序连续子数组](https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shuan-e1le/) | 中等 | 🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
| [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-bits/solution/yi-ti-san-jie-dui-cheng-wei-zhu-wei-fen-ub1hi/) | 简单 | 🤩🤩🤩 |
2525
| [233. 数字 1 的个数](https://leetcode-cn.com/problems/number-of-digit-one/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-digit-one/solution/gong-shui-san-xie-jiang-shu-wei-dp-wen-t-c9oi/) | 困难 | 🤩🤩🤩🤩 |
2626
| [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ugly-number/solution/gong-shui-san-xie-jian-dan-de-fen-qing-k-dlvg/) | 简单 | 🤩🤩 |
27+
| [284. 顶端迭代器](https://leetcode-cn.com/problems/peeking-iterator/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/peeking-iterator/solution/gong-shui-san-xie-die-dai-qi-ji-ben-ren-b77lz/) | 中等 | 🤩🤩🤩🤩 |
2728
| [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/solution/gong-shui-san-xie-note-bie-pian-shuang-z-c8ii/) | 简单 | 🤩🤩🤩 |
2829
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
2930
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |
31+
| [414. 第三大的数](https://leetcode-cn.com/problems/third-maximum-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/third-maximum-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-pmln/) | 中等 | 🤩🤩🤩🤩 |
3032
| [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/string-compression/solution/gong-shui-san-xie-shuang-zhi-zhen-yuan-d-bppu/) | 中等 | 🤩🤩🤩🤩 |
3133
| [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-characters-by-frequency/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-gst9/) | 中等 | 🤩🤩🤩🤩 |
3234
| [457. 环形数组是否存在循环](https://leetcode-cn.com/problems/circular-array-loop/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/circular-array-loop/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-ag05/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[414. 第三大的数](https://leetcode-cn.com/problems/third-maximum-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-pmln/)** ,难度为 **简单**
4+
5+
Tag : 「排序」、「数组」、「模拟」
6+
7+
给你一个非空数组,返回此数组中 第三大的数 。如果不存在,则返回数组中最大的数。
8+
9+
示例 1:
10+
```
11+
输入:[3, 2, 1]
12+
13+
输出:1
14+
15+
解释:第三大的数是 1 。
16+
```
17+
示例 2:
18+
```
19+
输入:[1, 2]
20+
21+
输出:2
22+
23+
解释:第三大的数不存在, 所以返回最大的数 2 。
24+
```
25+
示例 3:
26+
```
27+
输入:[2, 2, 3, 1]
28+
29+
输出:1
30+
31+
解释:注意,要求返回第三大的数,是指在所有不同数字中排第三大的数。
32+
此例中存在两个值为 2 的数,它们都排第二。在所有不同数字中排第三大的数为 1 。
33+
```
34+
35+
提示:
36+
* 1 <= nums.length <= 10ドル^4$
37+
* $-2^{31}$ <= nums[i] <= 2ドル^{31} - 1$
38+
39+
**进阶:你能设计一个时间复杂度 O(n) 的解决方案吗?**
40+
41+
---
42+
43+
### Set 去重 + 排序
44+
45+
题目要求返回含重复元素的数组 $nums$ 中的第三大数。
46+
47+
一个朴素的做法是,先使用 `Set` 对重复元素进行去重,然后对去重后的元素进行排序,并返回第三大的元素。
48+
49+
代码:
50+
```Java
51+
class Solution {
52+
public int thirdMax(int[] nums) {
53+
Set<Integer> set = new HashSet<>();
54+
for (int x : nums) set.add(x);
55+
List<Integer> list = new ArrayList<>(set);
56+
Collections.sort(list);
57+
return list.size() < 3 ? list.get(list.size() - 1) : list.get(list.size() - 3);
58+
}
59+
}
60+
```
61+
* 时间复杂度:使用 `Set` 去重的复杂度为 $O(n)$;排序复杂度为 $O(n\log{n})$。整体复杂度为 $O(n\log{n})$
62+
* 空间复杂度:$O(n)$
63+
64+
---
65+
66+
### 有限变量 + 遍历
67+
68+
**经典的找数组次大值的做法是使用两个变量 `a``b` 分别存储遍历过程中的最大值和次大值。**
69+
70+
假设当前遍历到的元素为 $x,ドル当满足如下条件时,考虑更新 `a` 或者 `b`:
71+
1. 当 $x > a$ 时,说明最大值被更新,同时原来的最大值沦为次大值。即有 $b = a; a = x;$
72+
2. 在条件 1ドル$ 不满足,且有$x > b$ 时,此时可以根据是否有「严格次大值」的要求,而决定是否要增加 $x < a$ 的条件:
73+
* 不要求为「严格次大值」:直接使用 $x$ 来更新 `b`,即有 $b = x$;
74+
* 当要求为「严格次大值」: 此时需要满足 $x < a$ 的条件,才能更新 `b`
75+
76+
回到本题,同理我们可以使用 `a``b``c` 三个变量来代指「最大值」、「严格次大值」和「严格第三大值」。
77+
78+
从前往后遍历 $nums,ドル假设当前元素为 $x,ドル对是否更新三者进行分情况讨论(判断优先级从上往下):
79+
80+
1. $x > a,ドル说明最大值被更新,将原本的「最大值」和「次大值」往后顺延为「次大值」和「第三大值」,并用 $x$ 更新 `a`;
81+
2. $x < a$ 且 $x > b,ドル说明次大值被更新,将原本的「次大值」往后顺延为「第三大值」,并用 $x$ 更新 `b`;
82+
3. $x < b$ 且 $x > c,ドル说明第三大值被更新,使用 $x$ 更新 `c`
83+
84+
起始时,我们希望使用一个足够小的数来初始化 `a``b``c`,但由于 $num[i]$ 的范围为 $[-2^{31}, 2^{31} - 1],ドル因此需要使用 `long` 来进行代替。
85+
86+
返回时,通过判断第三大值是否为初始化时的负无穷,来得知是否存在第三大值。
87+
88+
代码:
89+
```Java
90+
class Solution {
91+
long INF = (long)-1e18;
92+
public int thirdMax(int[] nums) {
93+
long a = INF, b = INF, c = INF;
94+
for (int x : nums) {
95+
if (x > a) {
96+
c = b; b = a; a = x;
97+
} else if (x < a && x > b) {
98+
c = b; b = x;
99+
} else if (x < b && x > c) {
100+
c = x;
101+
}
102+
}
103+
return c != INF ? (int)c : (int)a;
104+
}
105+
}
106+
```
107+
* 时间复杂度:$O(n)$
108+
* 空间复杂度:$O(1)$
109+
110+
---
111+
112+
### 最后
113+
114+
这是我们「刷穿 LeetCode」系列文章的第 `No.414` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
115+
116+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
117+
118+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
119+
120+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
121+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /