-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
311 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# [1344. 时钟指针的夹角](https://leetcode.cn/problems/angle-between-hands-of-a-clock/) | ||
|
||
- 标签:数学 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1344. 时钟指针的夹角 - 力扣](https://leetcode.cn/problems/angle-between-hands-of-a-clock/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定两个数 $hour$ 和 $minutes$。 | ||
|
||
**要求**:请你返回在时钟上,由给定时间的时针和分针组成的较小角的角度($60$ 单位制)。 | ||
|
||
**说明**: | ||
|
||
- $1 \le hour \le 12$。 | ||
- $0 \le minutes \le 59$。 | ||
- 与标准答案误差在 $10^{-5}$ 以内的结果都被视为正确结果。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/08/sample_1_1673.png) | ||
|
||
```python | ||
输入:hour = 12, minutes = 30 | ||
输出:165 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/08/sample_2_1673.png) | ||
|
||
```python | ||
输入:hour = 3, minutes = 30 | ||
输出;75 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:数学 | ||
|
||
1. 我们以 $00:00$ 为基准,分别计算出分针与 $00:00$ 中垂线的夹角,以及时针与 $00:00$ 中垂线的夹角。 | ||
2. 然后计算出两者差值的绝对值 $diff$。当前差值可能为较小的角(小于 $180°$ 的角),也可能为较大的角(大于等于 $180°$ 的角)。 | ||
3. 将差值的绝对值 $diff$ 与 $360 - diff$ 进行比较,取较小值作为答案。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def angleClock(self, hour: int, minutes: int) -> float: | ||
mins_angle = 6 * minutes | ||
hours_angle = (hour % 12 + minutes / 60) * 30 | ||
|
||
diff = abs(hours_angle - mins_angle) | ||
return min(diff, 360 - diff) | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(1)$。 | ||
- **空间复杂度**:$O(1)$。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# [1582. 二进制矩阵中的特殊位置](https://leetcode.cn/problems/special-positions-in-a-binary-matrix/) | ||
|
||
- 标签:数组、矩阵 | ||
- 难度:简单 | ||
|
||
## 题目链接 | ||
|
||
- [1582. 二进制矩阵中的特殊位置 - 力扣](https://leetcode.cn/problems/special-positions-in-a-binary-matrix/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个 $m \times n$ 的二进制矩阵 $mat$。 | ||
|
||
**要求**:返回矩阵 $mat$ 中特殊位置的数量。 | ||
|
||
**说明**: | ||
|
||
- **特殊位置**:如果位置 $(i, j)$ 满足 $mat[i][j] == 1$ 并且行 $i$ 与列 $j$ 中的所有其他元素都是 $0$(行和列的下标从 $0$ 开始计数),那么它被称为特殊位置。 | ||
- $m == mat.length$。 | ||
- $n == mat[i].length$。 | ||
- $1 \le m, n \le 100$。 | ||
- $mat[i][j]$ 是 $0$ 或 $1$。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
![](https://assets.leetcode.com/uploads/2021/12/23/special1.jpg) | ||
|
||
```python | ||
输入:mat = [[1,0,0],[0,0,1],[1,0,0]] | ||
输出:1 | ||
解释:位置 (1, 2) 是一个特殊位置,因为 mat[1][2] == 1 且第 1 行和第 2 列的其他所有元素都是 0。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
![img](https://assets.leetcode.com/uploads/2021/12/24/special-grid.jpg) | ||
|
||
```python | ||
输入:mat = [[1,0,0],[0,1,0],[0,0,1]] | ||
输出:3 | ||
解释:位置 (0, 0),(1, 1) 和 (2, 2) 都是特殊位置。 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:模拟 | ||
|
||
1. 按照行、列遍历二位数组 $mat$。 | ||
2. 使用数组 $row\underline{}cnts$、$col\underline{}cnts$ 分别记录每行和每列所含 $1$ 的个数。 | ||
3. 再次按照行、列遍历二维数组 $mat$。 | ||
4. 统计满足 $mat[row][col] == 1$ 并且 $row\underline{}cnts[row] == col\underline{}cnts[col] == 1$ 的位置个数。 | ||
5. 返回答案。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def numSpecial(self, mat: List[List[int]]) -> int: | ||
rows, cols = len(mat), len(mat[0]) | ||
row_cnts = [0 for _ in range(rows)] | ||
col_cnts = [0 for _ in range(cols)] | ||
|
||
for row in range(rows): | ||
for col in range(cols): | ||
row_cnts[row] += mat[row][col] | ||
col_cnts[col] += mat[row][col] | ||
|
||
ans = 0 | ||
for row in range(rows): | ||
for col in range(cols): | ||
if mat[row][col] == 1 and row_cnts[row] == 1 and col_cnts[col] == 1: | ||
ans += 1 | ||
|
||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(m \times n)$,其中 $m$、$n$ 分别为数组 $mat$ 的行数和列数。 | ||
- **空间复杂度**:$O(m + n)$。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# [1614. 括号的最大嵌套深度](https://leetcode.cn/problems/maximum-nesting-depth-of-the-parentheses/) | ||
|
||
- 标签:栈、字符串 | ||
- 难度:简单 | ||
|
||
## 题目链接 | ||
|
||
- [1614. 括号的最大嵌套深度 - 力扣](https://leetcode.cn/problems/maximum-nesting-depth-of-the-parentheses/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给你一个有效括号字符串 $s$。 | ||
|
||
**要求**:返回该字符串 $s$ 的嵌套深度 。 | ||
|
||
**说明**: | ||
|
||
- 如果字符串满足以下条件之一,则可以称之为 有效括号字符串(valid parentheses string,可以简写为 VPS): | ||
- 字符串是一个空字符串 `""`,或者是一个不为 `"("` 或 `")"` 的单字符。 | ||
- 字符串可以写为 $AB$($A$ 与 B 字符串连接),其中 $A$ 和 $B$ 都是有效括号字符串 。 | ||
- 字符串可以写为 ($A$),其中 $A$ 是一个有效括号字符串。 | ||
|
||
- 类似地,可以定义任何有效括号字符串 $s$ 的 嵌套深度 $depth(s)$: | ||
|
||
- `depth("") = 0`。 | ||
- `depth(C) = 0`,其中 $C$ 是单个字符的字符串,且该字符不是 `"("` 或者 `")"`。 | ||
- `depth(A + B) = max(depth(A), depth(B))`,其中 $A$ 和 $B$ 都是 有效括号字符串。 | ||
- `depth("(" + A + ")") = 1 + depth(A)`,其中 A 是一个 有效括号字符串。 | ||
- $1 \le s.length \le 100$。 | ||
- $s$ 由数字 $0 \sim 9$ 和字符 `'+'`、`'-'`、`'*'`、`'/'`、`'('`、`')'` 组成。 | ||
- 题目数据保证括号表达式 $s$ 是有效的括号表达式。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入:s = "(1+(2*3)+((8)/4))+1" | ||
输出:3 | ||
解释:数字 8 在嵌套的 3 层括号中。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:s = "(1)+((2))+(((3)))" | ||
输出:3 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:模拟 | ||
|
||
我们可以使用栈来进行模拟括号匹配。遍历字符串 $s$,如果遇到左括号,则将其入栈,如果遇到右括号,则弹出栈中的左括号,与当前右括号进行匹配。在整个过程中栈的大小的最大值,就是我们要求的 $s$ 的嵌套深度,其实也是求最大的连续左括号的数量(跳过普通字符,并且与右括号匹配后)。具体步骤如下: | ||
|
||
1. 使用 $ans$ 记录最大的连续左括号数量,使用 $cnt$ 记录当前栈中左括号的数量。 | ||
2. 遍历字符串 $s$: | ||
1. 如果遇到左括号,则令 $cnt$ 加 $1$。 | ||
2. 如果遇到右括号,则令 $cnt$ 减 $1$。 | ||
3. 将 $cnt$ 与答案进行比较,更新最大的连续左括号数量。 | ||
3. 遍历完字符串 $s$,返回答案 $ans$。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def maxDepth(self, s: str) -> int: | ||
ans, cnt = 0, 0 | ||
for ch in s: | ||
if ch == '(': | ||
cnt += 1 | ||
elif ch == ')': | ||
cnt -= 1 | ||
ans = max(ans, cnt) | ||
|
||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n)$,其中 $n$ 为字符串 $s$ 的长度。 | ||
- **空间复杂度**:$O(1)$。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# [1936. 新增的最少台阶数](https://leetcode.cn/problems/add-minimum-number-of-rungs/) | ||
|
||
- 标签:贪心、数组 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1936. 新增的最少台阶数 - 力扣](https://leetcode.cn/problems/add-minimum-number-of-rungs/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个严格递增的整数数组 $rungs$,用于表示梯子上每一台阶的高度。当前你正站在高度为 $0$ 的地板上,并打算爬到最后一个台阶。 | ||
|
||
另给定一个整数 $dist$。每次移动中,你可以到达下一个距离当前位置(地板或台阶)不超过 $dist$ 高度的台阶。当前,你也可以在任何正整数高度插入尚不存在的新台阶。 | ||
|
||
**要求**:返回爬到最后一阶时必须添加到梯子上的最少台阶数。 | ||
|
||
**说明**: | ||
|
||
- | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入:rungs = [1,3,5,10], dist = 2 | ||
输出:2 | ||
解释: | ||
现在无法到达最后一阶。 | ||
在高度为 7 和 8 的位置增设新的台阶,以爬上梯子。 | ||
梯子在高度为 [1,3,5,7,8,10] 的位置上有台阶。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:rungs = [3,4,6,7], dist = 2 | ||
输出:1 | ||
解释: | ||
现在无法从地板到达梯子的第一阶。 | ||
在高度为 1 的位置增设新的台阶,以爬上梯子。 | ||
梯子在高度为 [1,3,4,6,7] 的位置上有台阶。 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:贪心算法 + 模拟 | ||
|
||
1. 遍历梯子的每一层台阶。 | ||
2. 计算每一层台阶与上一层台阶之间的差值 $diff$。 | ||
3. 每层最少需要新增的台阶数为 $\lfloor \frac{diff - 1}{dist} \rfloor$,将其计入答案 $ans$ 中。 | ||
4. 遍历完返回答案。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def addRungs(self, rungs: List[int], dist: int) -> int: | ||
ans, cur = 0, 0 | ||
for h in rungs: | ||
diff = h - cur | ||
ans += (diff - 1) // dist | ||
cur = h | ||
|
||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n)$,其中 $n$ 为数组 $rungs$ 的长度。 | ||
- **空间复杂度**:$O(1)$。 | ||
|