Skip to content

Commit 1dd4a74

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0739
See https://leetcode-cn.com/problems/daily-temperatures
1 parent 191aa10 commit 1dd4a74

File tree

7 files changed

+82
-12
lines changed

7 files changed

+82
-12
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
1. [用队列实现栈](/solution/0200-0299/0225.Implement%20Stack%20using%20Queues/README.md)
100100
1. [逆波兰表达式求值](/solution/0100-0199/0150.Evaluate%20Reverse%20Polish%20Notation/README.md)
101101
1. [最近的请求次数](/solution/0900-0999/0933.Number%20of%20Recent%20Calls/README.md)
102+
1. [每日温度](/solution/0700-0799/0739.Daily%20Temperatures/README.md)
102103

103104
### 动态规划
104105

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
9494
1. [Implement Stack using Queues](/solution/0200-0299/0225.Implement%20Stack%20using%20Queues/README_EN.md)
9595
1. [Evaluate Reverse Polish Notation](/solution/0100-0199/0150.Evaluate%20Reverse%20Polish%20Notation/README_EN.md)
9696
1. [Number of Recent Calls](/solution/0900-0999/0933.Number%20of%20Recent%20Calls/README_EN.md)
97+
1. [Daily Temperatures](/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md)
9798

9899
### Dynamic Programming
99100

solution/0700-0799/0739.Daily Temperatures/README.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,55 @@
1515

1616
<!-- 这里可写通用的实现逻辑 -->
1717

18+
栈实现,栈存放 T 中元素的的下标 i,结果用数组 res 存储。
19+
20+
遍历 T,遍历到 `T[i]` 时:
21+
22+
- 若栈不为空,并且栈顶元素小于 `T[i]` 时,弹出栈顶元素 j,并且 `res[j]` 赋值为 `i - j`
23+
- 然后将 i 压入栈中。
24+
25+
最后返回结果数组 res 即可。
26+
1827
<!-- tabs:start -->
1928

2029
### **Python3**
2130

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

2433
```python
25-
34+
class Solution:
35+
def dailyTemperatures(self, T: List[int]) -> List[int]:
36+
n = len(T)
37+
res = [0 for _ in range(n)]
38+
s = []
39+
for i in range(n):
40+
while s and T[s[-1]] < T[i]:
41+
j = s.pop()
42+
res[j] = i - j
43+
s.append(i)
44+
return res
2645
```
2746

2847
### **Java**
2948

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

3251
```java
33-
52+
class Solution {
53+
public int[] dailyTemperatures(int[] T) {
54+
int n = T.length;
55+
int[] res = new int[n];
56+
Deque<Integer> s = new ArrayDeque<>();
57+
for (int i = 0; i < n; ++i) {
58+
while (!s.isEmpty() && T[s.peek()] < T[i]) {
59+
int j = s.pop();
60+
res[j] = i - j;
61+
}
62+
s.push(i);
63+
}
64+
return res;
65+
}
66+
}
3467
```
3568

3669
### **...**

solution/0700-0799/0739.Daily Temperatures/README_EN.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,37 @@ Each temperature will be an integer in the range <code>[30, 100]</code>.
2929
### **Python3**
3030

3131
```python
32-
32+
class Solution:
33+
def dailyTemperatures(self, T: List[int]) -> List[int]:
34+
n = len(T)
35+
res = [0 for _ in range(n)]
36+
s = []
37+
for i in range(n):
38+
while s and T[s[-1]] < T[i]:
39+
j = s.pop()
40+
res[j] = i - j
41+
s.append(i)
42+
return res
3343
```
3444

3545
### **Java**
3646

3747
```java
38-
48+
class Solution {
49+
public int[] dailyTemperatures(int[] T) {
50+
int n = T.length;
51+
int[] res = new int[n];
52+
Deque<Integer> s = new ArrayDeque<>();
53+
for (int i = 0; i < n; ++i) {
54+
while (!s.isEmpty() && T[s.peek()] < T[i]) {
55+
int j = s.pop();
56+
res[j] = i - j;
57+
}
58+
s.push(i);
59+
}
60+
return res;
61+
}
62+
}
3963
```
4064

4165
### **...**

solution/0700-0799/0739.Daily Temperatures/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ class Solution {
22
public int[] dailyTemperatures(int[] T) {
33
int n = T.length;
44
int[] res = new int[n];
5-
ArrayDeque<Integer> stack = new ArrayDeque<>();
5+
Deque<Integer> s = new ArrayDeque<>();
66
for (int i = 0; i < n; ++i) {
7-
while (!stack.isEmpty() && T[stack.peek()] < T[i]) {
8-
res[stack.peek()] = i - stack.peek();
9-
stack.pop();
7+
while (!s.isEmpty() && T[s.peek()] < T[i]) {
8+
int j = s.pop();
9+
res[j] = i - j;
1010
}
11-
stack.push(i);
11+
s.push(i);
1212
}
1313
return res;
1414
}
15-
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def dailyTemperatures(self, T: List[int]) -> List[int]:
3+
n = len(T)
4+
res = [0 for _ in range(n)]
5+
s = []
6+
for i in range(n):
7+
while s and T[s[-1]] < T[i]:
8+
j = s.pop()
9+
res[j] = i - j
10+
s.append(i)
11+
return res

solution/0900-0999/0933.Number of Recent Calls/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41-
<!-- tabs:start -->
42-
4341
在第 1、100、3001、3002 这四个时间点分别进行了 ping 请求, 在 3001 秒的时候, 它前面的 3000 秒指的是区间 `[1,3001]`, 所以一共是有 `1、100、3001` 三个请求, t = 3002 的前 3000 秒指的是区间 `[2,3002]`, 所以有 `100、3001、3002` 三次请求。
4442

4543
可以用队列实现。每次将 t 进入队尾,同时从队头开始依次移除小于 `t-3000` 的元素。然后返回队列的大小 `q.size()` 即可。
4644

45+
<!-- tabs:start -->
46+
4747
### **Python3**
4848

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

0 commit comments

Comments
 (0)