Skip to content

Commit c3386dc

Browse files
committed
feat: add solutions to lcof2 problem: No.038
1 parent 63410c9 commit c3386dc

File tree

8 files changed

+143
-22
lines changed

8 files changed

+143
-22
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- Gitee Pages: https://doocs.gitee.io/leetcode
2525
- GitHub Pages: https://doocs.github.io/leetcode
2626

27+
注:😶本项目被 Gitee 官方误判为“包含违禁违规内容”,导致 Gitee Pages 404。我们正在尝试联系 Gitee 官方人员处理,请朋友们优先访问 Netlify / GitHub Pages 站点。
28+
2729
## LeetCode 全解
2830

2931
- [LeetCode](./solution/README.md)

lcof2/剑指 Offer II 038. 每日温度/README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,98 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
单调栈常见模型:找出每个数左/右边**离它最近的****比它大/小的数**。模板:
50+
51+
```python
52+
stk = []
53+
for i in range(n):
54+
while stk and check(stk[-1], i):
55+
stk.pop()
56+
stk.append(i)
57+
```
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
5262

5363
<!-- 这里可写当前语言的特殊实现逻辑 -->
5464

5565
```python
56-
66+
class Solution:
67+
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
68+
res = [0] * len(temperatures)
69+
stk = []
70+
for i, t in enumerate(temperatures):
71+
while stk and temperatures[stk[-1]] < t:
72+
j = stk.pop()
73+
res[j] = i - j
74+
stk.append(i)
75+
return res
5776
```
5877

5978
### **Java**
6079

6180
<!-- 这里可写当前语言的特殊实现逻辑 -->
6281

6382
```java
83+
class Solution {
84+
public int[] dailyTemperatures(int[] temperatures) {
85+
int n = temperatures.length;
86+
int[] res = new int[n];
87+
Deque<Integer> stk = new ArrayDeque<>();
88+
for (int i = 0; i < n; ++i) {
89+
while (!stk.isEmpty() && temperatures[stk.peek()] < temperatures[i]) {
90+
int j = stk.pop();
91+
res[j] = i - j;
92+
}
93+
stk.push(i);
94+
}
95+
return res;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
<!-- 这里可写当前语言的特殊实现逻辑 -->
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
vector<int> dailyTemperatures(vector<int> &temperatures) {
108+
int n = temperatures.size();
109+
vector<int> res(n);
110+
stack<int> stk;
111+
for (int i = 0; i < n; ++i)
112+
{
113+
while (!stk.empty() && temperatures[stk.top()] < temperatures[i])
114+
{
115+
res[stk.top()] = i - stk.top();
116+
stk.pop();
117+
}
118+
stk.push(i);
119+
}
120+
return res;
121+
}
122+
};
123+
```
64124
125+
### **Go**
126+
127+
```go
128+
func dailyTemperatures(temperatures []int) []int {
129+
res := make([]int, len(temperatures))
130+
var stk []int
131+
for i, t := range temperatures {
132+
for len(stk) > 0 && temperatures[stk[len(stk)-1]] < t {
133+
j := stk[len(stk)-1]
134+
res[j] = i - j
135+
stk = stk[:len(stk)-1]
136+
}
137+
stk = append(stk, i)
138+
}
139+
return res
140+
}
65141
```
66142

67143
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> dailyTemperatures(vector<int> &temperatures) {
4+
int n = temperatures.size();
5+
vector<int> res(n);
6+
stack<int> stk;
7+
for (int i = 0; i < n; ++i)
8+
{
9+
while (!stk.empty() && temperatures[stk.top()] < temperatures[i])
10+
{
11+
res[stk.top()] = i - stk.top();
12+
stk.pop();
13+
}
14+
stk.push(i);
15+
}
16+
return res;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func dailyTemperatures(temperatures []int) []int {
2+
res := make([]int, len(temperatures))
3+
var stk []int
4+
for i, t := range temperatures {
5+
for len(stk) > 0 && temperatures[stk[len(stk)-1]] < t {
6+
j := stk[len(stk)-1]
7+
res[j] = i - j
8+
stk = stk[:len(stk)-1]
9+
}
10+
stk = append(stk, i)
11+
}
12+
return res
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] dailyTemperatures(int[] temperatures) {
3+
int n = temperatures.length;
4+
int[] res = new int[n];
5+
Deque<Integer> stk = new ArrayDeque<>();
6+
for (int i = 0; i < n; ++i) {
7+
while (!stk.isEmpty() && temperatures[stk.peek()] < temperatures[i]) {
8+
int j = stk.pop();
9+
res[j] = i - j;
10+
}
11+
stk.push(i);
12+
}
13+
return res;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
3+
res = [0] * len(temperatures)
4+
stk = []
5+
for i, t in enumerate(temperatures):
6+
while stk and temperatures[stk[-1]] < t:
7+
j = stk.pop()
8+
res[j] = i - j
9+
stk.append(i)
10+
return res

lcof2/剑指 Offer II 072. 求平方根/README.md

+7-20
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,26 @@
44

55
<!-- 这里写题目描述 -->
66

7-
<p>给定一个非负整数 <code>x</code> ,计算并返回 <code>x</code> 的平方根,即实现&nbsp;<code>int sqrt(int x)</code>&nbsp;函数。</p>
7+
<p>实现&nbsp;<code>int sqrt(int x)</code>&nbsp;函数。</p>
88

9-
<p>正数的平方根有两个,只输出其中的正数平方根。</p>
9+
<p>计算并返回&nbsp;<em>x</em>&nbsp;的平方根,其中&nbsp;<em>x </em>是非负整数。</p>
1010

11-
<p>如果平方根不是整数,输出只保留整数的部分,小数部分将被舍去。</p>
12-
13-
<p>&nbsp;</p>
11+
<p>由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。</p>
1412

1513
<p><strong>示例 1:</strong></p>
1614

17-
<pre>
18-
<strong>输入:</strong> x = 4
15+
<pre><strong>输入:</strong> 4
1916
<strong>输出:</strong> 2
2017
</pre>
2118

2219
<p><strong>示例 2:</strong></p>
2320

24-
<pre>
25-
<strong>输入:</strong> x = 8
21+
<pre><strong>输入:</strong> 8
2622
<strong>输出:</strong> 2
27-
<strong>解释:</strong> 8 的平方根是 2.82842...,由于小数部分将被舍去,所以返回 2
23+
<strong>说明:</strong> 根号 8 是 2.82842...,
24+
&nbsp; 由于返回类型是整数,小数部分将被舍去。
2825
</pre>
2926

30-
<p>&nbsp;</p>
31-
32-
<p><strong>提示:</strong></p>
33-
34-
<ul>
35-
<li><meta charset="UTF-8" /><code>0 &lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li>
36-
</ul>
37-
38-
<p>&nbsp;</p>
39-
4027
<p><meta charset="UTF-8" />注意:本题与主站 69&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/sqrtx/">https://leetcode-cn.com/problems/sqrtx/</a></p>
4128

4229

solution/0000-0099/0069.Sqrt(x)/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<pre><strong>输入:</strong> 8
2424
<strong>输出:</strong> 2
25-
<strong>说明:</strong> 8 的平方根是 2.82842...,
25+
<strong>说明:</strong> 根号 8 是 2.82842...,
2626
&nbsp; 由于返回类型是整数,小数部分将被舍去。
2727
</pre>
2828

0 commit comments

Comments
 (0)