Skip to content

Commit 2df6274

Browse files
committed
更新题解列表
1 parent c5354d4 commit 2df6274

File tree

4 files changed

+165
-2
lines changed

4 files changed

+165
-2
lines changed

Contents/00.Introduction/04.Solutions-List.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 854 道)
1+
# LeetCode 题解(已完成 856 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -584,6 +584,7 @@
584584
| 1486 | [数组异或操作](https://leetcode.cn/problems/xor-operation-in-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1486.%20%E6%95%B0%E7%BB%84%E5%BC%82%E6%88%96%E6%93%8D%E4%BD%9C.md) | 位运算、数学 | 简单 |
585585
| 1491 | [去掉最低工资和最高工资后的工资平均值](https://leetcode.cn/problems/average-salary-excluding-the-minimum-and-maximum-salary/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1491.%20%E5%8E%BB%E6%8E%89%E6%9C%80%E4%BD%8E%E5%B7%A5%E8%B5%84%E5%92%8C%E6%9C%80%E9%AB%98%E5%B7%A5%E8%B5%84%E5%90%8E%E7%9A%84%E5%B7%A5%E8%B5%84%E5%B9%B3%E5%9D%87%E5%80%BC.md) | 数组、排序 | 简单 |
586586
| 1493 | [删掉一个元素以后全为 1 的最长子数组](https://leetcode.cn/problems/longest-subarray-of-1s-after-deleting-one-element/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1493.%20%E5%88%A0%E6%8E%89%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E4%BB%A5%E5%90%8E%E5%85%A8%E4%B8%BA%201%20%E7%9A%84%E6%9C%80%E9%95%BF%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、动态规划、滑动窗口 | 中等 |
587+
| 1496 | [判断路径是否相交](https://leetcode.cn/problems/path-crossing/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1496.%20%E5%88%A4%E6%96%AD%E8%B7%AF%E5%BE%84%E6%98%AF%E5%90%A6%E7%9B%B8%E4%BA%A4.md) | 哈希表、字符串 | 简单 |
587588
| 1502 | [判断能否形成等差数列](https://leetcode.cn/problems/can-make-arithmetic-progression-from-sequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1502.%20%E5%88%A4%E6%96%AD%E8%83%BD%E5%90%A6%E5%BD%A2%E6%88%90%E7%AD%89%E5%B7%AE%E6%95%B0%E5%88%97.md) | 数组、排序 | 简单 |
588589
| 1507 | [转变日期格式](https://leetcode.cn/problems/reformat-date/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1507.%20%E8%BD%AC%E5%8F%98%E6%97%A5%E6%9C%9F%E6%A0%BC%E5%BC%8F.md) | 字符串 | 简单 |
589590
| 1523 | [在区间范围内统计奇数数目](https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1523.%20%E5%9C%A8%E5%8C%BA%E9%97%B4%E8%8C%83%E5%9B%B4%E5%86%85%E7%BB%9F%E8%AE%A1%E5%A5%87%E6%95%B0%E6%95%B0%E7%9B%AE.md) | 数学 | 简单 |
@@ -603,6 +604,7 @@
603604
| 1631 | [最小体力消耗路径](https://leetcode.cn/problems/path-with-minimum-effort/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1631.%20%E6%9C%80%E5%B0%8F%E4%BD%93%E5%8A%9B%E6%B6%88%E8%80%97%E8%B7%AF%E5%BE%84.md) | 深度优先搜索、广度优先搜索、并查集、数组、二分查找、矩阵、堆(优先队列) | 中等 |
604605
| 1641 | [统计字典序元音字符串的数目](https://leetcode.cn/problems/count-sorted-vowel-strings/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1641.%20%E7%BB%9F%E8%AE%A1%E5%AD%97%E5%85%B8%E5%BA%8F%E5%85%83%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%95%B0%E7%9B%AE.md) | 数学、动态规划、组合数学 | 中等 |
605606
| 1646 | [获取生成数组中的最大值](https://leetcode.cn/problems/get-maximum-in-generated-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1646.%20%E8%8E%B7%E5%8F%96%E7%94%9F%E6%88%90%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC.md) | 数组、动态规划、模拟 | 简单 |
607+
| 1647 | [字符频次唯一的最小删除次数](https://leetcode.cn/problems/minimum-deletions-to-make-character-frequencies-unique/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1647.%20%E5%AD%97%E7%AC%A6%E9%A2%91%E6%AC%A1%E5%94%AF%E4%B8%80%E7%9A%84%E6%9C%80%E5%B0%8F%E5%88%A0%E9%99%A4%E6%AC%A1%E6%95%B0.md) | 贪心、哈希表、字符串、排序 | 中等 |
606608
| 1657 | [确定两个字符串是否接近](https://leetcode.cn/problems/determine-if-two-strings-are-close/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1657.%20%E7%A1%AE%E5%AE%9A%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%8E%A5%E8%BF%91.md) | 哈希表、字符串、排序 | 中等 |
607609
| 1658 | [将 x 减到 0 的最小操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1658.%20%E5%B0%86%20x%20%E5%87%8F%E5%88%B0%200%20%E7%9A%84%E6%9C%80%E5%B0%8F%E6%93%8D%E4%BD%9C%E6%95%B0.md) | 数组、哈希表、二分查找、前缀和、滑动窗口 | 中等 |
608610
| 1672 | [最富有客户的资产总量](https://leetcode.cn/problems/richest-customer-wealth/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1672.%20%E6%9C%80%E5%AF%8C%E6%9C%89%E5%AE%A2%E6%88%B7%E7%9A%84%E8%B5%84%E4%BA%A7%E6%80%BB%E9%87%8F.md) | 数组、矩阵 | 简单 |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,4 @@
255255
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)
256256

257257
## 11. 附加内容
258-
## [12. LeetCode 题解(已完成 854 道)](./Contents/00.Introduction/04.Solutions-List.md)
258+
## [12. LeetCode 题解(已完成 856 道)](./Contents/00.Introduction/04.Solutions-List.md)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [1496. 判断路径是否相交](https://leetcode.cn/problems/path-crossing/)
2+
3+
- 标签:哈希表、字符串
4+
- 难度:简单
5+
6+
## 题目链接
7+
8+
- [1496. 判断路径是否相交 - 力扣](https://leetcode.cn/problems/path-crossing/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个字符串 $path$,其中 $path[i]$ 的值可以是 `'N'``'S'``'E'` 或者 `'W'`,分别表示向北、向南、向东、向西移动一个单位。
13+
14+
你从二维平面上的原点 $(0, 0)$ 处开始出发,按 $path$ 所指示的路径行走。
15+
16+
**要求**:如果路径在任何位置上与自身相交,也就是走到之前已经走过的位置,请返回 $True$;否则,返回 $False$。
17+
18+
**说明**
19+
20+
- $1 \le path.length \le 10^4$。
21+
- $path[i]$ 为 `'N'``'S'``'E'``'W'`
22+
23+
**示例**
24+
25+
- 示例 1:
26+
27+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/28/screen-shot-2020-06-10-at-123929-pm.png)
28+
29+
```python
30+
输入:path = "NES"
31+
输出:false
32+
解释:该路径没有在任何位置相交。
33+
```
34+
35+
- 示例 2:
36+
37+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/28/screen-shot-2020-06-10-at-123843-pm.png)
38+
39+
```python
40+
输入:path = "NESWW"
41+
输出:true
42+
解释:该路径经过原点两次。
43+
```
44+
45+
## 解题思路
46+
47+
### 思路 1:哈希表 + 模拟
48+
49+
1. 使用哈希表将 `'N'``'S'``'E'``'W'` 对应横纵坐标轴上的改变表示出来。
50+
2. 使用集合 $visited$ 存储走过的坐标元组。
51+
3. 遍历 $path$,按照 $path$ 所指示的路径模拟行走,并将所走过的坐标使用 $visited$ 存储起来。
52+
4. 如果在 $visited$ 遇到已经走过的坐标,则返回 $True$。
53+
5. 如果遍历完仍未发现已经走过的坐标,则返回 $False$。
54+
55+
### 思路 1:代码
56+
57+
```Python
58+
class Solution:
59+
def isPathCrossing(self, path: str) -> bool:
60+
directions = {
61+
"N" : (-1, 0),
62+
"S" : (1, 0),
63+
"W" : (0, -1),
64+
"E" : (0, 1),
65+
}
66+
67+
x, y = 0, 0
68+
69+
visited = set()
70+
visited.add((x, y))
71+
72+
for ch in path:
73+
x += directions[ch][0]
74+
y += directions[ch][1]
75+
if (x, y) in visited:
76+
return True
77+
visited.add((x, y))
78+
79+
return False
80+
```
81+
82+
### 思路 1:复杂度分析
83+
84+
- **时间复杂度**:$O(n)$,其中 $n$ 为数组 $path$ 的长度。
85+
- **空间复杂度**:$O(n)$。
86+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [1647. 字符频次唯一的最小删除次数](https://leetcode.cn/problems/minimum-deletions-to-make-character-frequencies-unique/)
2+
3+
- 标签:贪心、哈希表、字符串、排序
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1647. 字符频次唯一的最小删除次数 - 力扣](https://leetcode.cn/problems/minimum-deletions-to-make-character-frequencies-unique/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个字符串 $s$。
13+
14+
**要求**:返回使 $s$ 成为优质字符串需要删除的最小字符数。
15+
16+
**说明**
17+
18+
- **频次**:指的是该字符在字符串中的出现次数。例如,在字符串 `"aab"` 中,`'a'` 的频次是 $2$,而 `'b'` 的频次是 $1$。
19+
- **优质字符串**:如果字符串 $s$ 中不存在两个不同字符频次相同的情况,就称 $s$ 是优质字符串。
20+
- $1 \le s.length \le 10^5$。
21+
- $s$ 仅含小写英文字母。
22+
23+
**示例**
24+
25+
- 示例 1:
26+
27+
```python
28+
输入:s = "aab"
29+
输出:0
30+
解释:s 已经是优质字符串。
31+
```
32+
33+
- 示例 2:
34+
35+
```python
36+
输入:s = "aaabbbcc"
37+
输出:2
38+
解释:可以删除两个 'b' , 得到优质字符串 "aaabcc"
39+
另一种方式是删除一个 'b' 和一个 'c' ,得到优质字符串 "aaabbc"
40+
```
41+
42+
## 解题思路
43+
44+
### 思路 1:贪心算法 + 哈希表
45+
46+
1. 使用哈希表 $cnts$ 统计每字符串中每个字符出现次数。
47+
2. 然后使用集合 $s\underline{}set$ 保存不同的出现次数。
48+
3. 遍历哈希表中所偶出现次数:
49+
1. 如果当前出现次数不在集合 $s\underline{}set$ 中,则将该次数添加到集合 $s\underline{}set$ 中。
50+
2. 如果当前出现次数在集合 $s\underline{}set$ 中,则不断减少该次数,直到该次数不在集合 $s\underline{}set$ 中停止,将次数添加到集合 $s\underline{}set$ 中,同时将减少次数累加到答案 $ans$ 中。
51+
4. 遍历完哈希表后返回答案 $ans$。
52+
53+
### 思路 1:代码
54+
55+
```Python
56+
class Solution:
57+
def minDeletions(self, s: str) -> int:
58+
cnts = Counter(s)
59+
s_set = set()
60+
61+
ans = 0
62+
for key, value in cnts.items():
63+
while value > 0 and value in s_set:
64+
value -= 1
65+
ans += 1
66+
s_set.add(value)
67+
68+
return ans
69+
```
70+
71+
### 思路 1:复杂度分析
72+
73+
- **时间复杂度**:$O(n)$。
74+
- **空间复杂度**:$O(n)$。
75+

0 commit comments

Comments
 (0)