Skip to content

Commit

Permalink
更新题解列表
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Jan 19, 2024
1 parent c5354d4 commit 2df6274
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Contents/00.Introduction/04.Solutions-List.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LeetCode 题解(已完成 854 道)
# LeetCode 题解(已完成 856 道)

| 题号 | 标题 | 题解 | 标签 | 难度 |
| :------ | :------ | :------ | :------ | :------ |
Expand Down Expand Up @@ -584,6 +584,7 @@
| 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) | 位运算、数学 | 简单 |
| 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) | 数组、排序 | 简单 |
| 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) | 数组、动态规划、滑动窗口 | 中等 |
| 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) | 哈希表、字符串 | 简单 |
| 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) | 数组、排序 | 简单 |
| 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) | 字符串 | 简单 |
| 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) | 数学 | 简单 |
Expand All @@ -603,6 +604,7 @@
| 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) | 深度优先搜索、广度优先搜索、并查集、数组、二分查找、矩阵、堆(优先队列) | 中等 |
| 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) | 数学、动态规划、组合数学 | 中等 |
| 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) | 数组、动态规划、模拟 | 简单 |
| 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) | 贪心、哈希表、字符串、排序 | 中等 |
| 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) | 哈希表、字符串、排序 | 中等 |
| 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) | 数组、哈希表、二分查找、前缀和、滑动窗口 | 中等 |
| 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) | 数组、矩阵 | 简单 |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,4 @@
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)

## 11. 附加内容
## [12. LeetCode 题解(已完成 854 道)](./Contents/00.Introduction/04.Solutions-List.md)
## [12. LeetCode 题解(已完成 856 道)](./Contents/00.Introduction/04.Solutions-List.md)
86 changes: 86 additions & 0 deletions Solutions/1496. 判断路径是否相交.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# [1496. 判断路径是否相交](https://leetcode.cn/problems/path-crossing/)

- 标签:哈希表、字符串
- 难度:简单

## 题目链接

- [1496. 判断路径是否相交 - 力扣](https://leetcode.cn/problems/path-crossing/)

## 题目大意

**描述**:给定一个字符串 $path$,其中 $path[i]$ 的值可以是 `'N'``'S'``'E'` 或者 `'W'`,分别表示向北、向南、向东、向西移动一个单位。

你从二维平面上的原点 $(0, 0)$ 处开始出发,按 $path$ 所指示的路径行走。

**要求**:如果路径在任何位置上与自身相交,也就是走到之前已经走过的位置,请返回 $True$;否则,返回 $False$。

**说明**

- $1 \le path.length \le 10^4$。
- $path[i]$ 为 `'N'``'S'``'E'``'W'`

**示例**

- 示例 1:

![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/28/screen-shot-2020-06-10-at-123929-pm.png)

```python
输入:path = "NES"
输出:false
解释:该路径没有在任何位置相交。
```

- 示例 2:

![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/28/screen-shot-2020-06-10-at-123843-pm.png)

```python
输入:path = "NESWW"
输出:true
解释:该路径经过原点两次。
```

## 解题思路

### 思路 1:哈希表 + 模拟

1. 使用哈希表将 `'N'``'S'``'E'``'W'` 对应横纵坐标轴上的改变表示出来。
2. 使用集合 $visited$ 存储走过的坐标元组。
3. 遍历 $path$,按照 $path$ 所指示的路径模拟行走,并将所走过的坐标使用 $visited$ 存储起来。
4. 如果在 $visited$ 遇到已经走过的坐标,则返回 $True$。
5. 如果遍历完仍未发现已经走过的坐标,则返回 $False$。

### 思路 1:代码

```Python
class Solution:
def isPathCrossing(self, path: str) -> bool:
directions = {
"N" : (-1, 0),
"S" : (1, 0),
"W" : (0, -1),
"E" : (0, 1),
}

x, y = 0, 0

visited = set()
visited.add((x, y))

for ch in path:
x += directions[ch][0]
y += directions[ch][1]
if (x, y) in visited:
return True
visited.add((x, y))

return False
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$,其中 $n$ 为数组 $path$ 的长度。
- **空间复杂度**:$O(n)$。

75 changes: 75 additions & 0 deletions Solutions/1647. 字符频次唯一的最小删除次数.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# [1647. 字符频次唯一的最小删除次数](https://leetcode.cn/problems/minimum-deletions-to-make-character-frequencies-unique/)

- 标签:贪心、哈希表、字符串、排序
- 难度:中等

## 题目链接

- [1647. 字符频次唯一的最小删除次数 - 力扣](https://leetcode.cn/problems/minimum-deletions-to-make-character-frequencies-unique/)

## 题目大意

**描述**:给定一个字符串 $s$。

**要求**:返回使 $s$ 成为优质字符串需要删除的最小字符数。

**说明**

- **频次**:指的是该字符在字符串中的出现次数。例如,在字符串 `"aab"` 中,`'a'` 的频次是 $2$,而 `'b'` 的频次是 $1$。
- **优质字符串**:如果字符串 $s$ 中不存在两个不同字符频次相同的情况,就称 $s$ 是优质字符串。
- $1 \le s.length \le 10^5$。
- $s$ 仅含小写英文字母。

**示例**

- 示例 1:

```python
输入:s = "aab"
输出:0
解释:s 已经是优质字符串。
```

- 示例 2:

```python
输入:s = "aaabbbcc"
输出:2
解释:可以删除两个 'b' , 得到优质字符串 "aaabcc"
另一种方式是删除一个 'b' 和一个 'c' ,得到优质字符串 "aaabbc"
```

## 解题思路

### 思路 1:贪心算法 + 哈希表

1. 使用哈希表 $cnts$ 统计每字符串中每个字符出现次数。
2. 然后使用集合 $s\underline{}set$ 保存不同的出现次数。
3. 遍历哈希表中所偶出现次数:
1. 如果当前出现次数不在集合 $s\underline{}set$ 中,则将该次数添加到集合 $s\underline{}set$ 中。
2. 如果当前出现次数在集合 $s\underline{}set$ 中,则不断减少该次数,直到该次数不在集合 $s\underline{}set$ 中停止,将次数添加到集合 $s\underline{}set$ 中,同时将减少次数累加到答案 $ans$ 中。
4. 遍历完哈希表后返回答案 $ans$。

### 思路 1:代码

```Python
class Solution:
def minDeletions(self, s: str) -> int:
cnts = Counter(s)
s_set = set()

ans = 0
for key, value in cnts.items():
while value > 0 and value in s_set:
value -= 1
ans += 1
s_set.add(value)

return ans
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$。
- **空间复杂度**:$O(n)$。

0 comments on commit 2df6274

Please sign in to comment.