Skip to content

Commit ab0eec6

Browse files
committed
feat: add solutions to lcof2 problem: No.038
剑指 Offer II 038. 每日温度
1 parent 19caf3b commit ab0eec6

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

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

+45
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,51 @@ func dailyTemperatures(temperatures []int) []int {
139139
}
140140
```
141141

142+
### **TypeScript**
143+
144+
```ts
145+
function dailyTemperatures(temperatures: number[]): number[] {
146+
const n = temperatures.length;
147+
const stack = [];
148+
const res = new Array(n);
149+
for (let i = n - 1; i >= 0; i--) {
150+
while (
151+
stack.length !== 0 &&
152+
temperatures[stack[stack.length - 1]] <= temperatures[i]
153+
) {
154+
stack.pop();
155+
}
156+
res[i] = stack.length === 0 ? 0 : stack[stack.length - 1] - i;
157+
stack.push(i);
158+
}
159+
return res;
160+
}
161+
```
162+
163+
### **Rust**
164+
165+
```rust
166+
impl Solution {
167+
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
168+
let n = temperatures.len();
169+
let mut stack = Vec::new();
170+
let mut res = vec![0; n];
171+
for i in (0..n).rev() {
172+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] <= temperatures[i] {
173+
stack.pop();
174+
}
175+
res[i] = if stack.is_empty() {
176+
0
177+
} else {
178+
(stack.last().unwrap() - i) as i32
179+
};
180+
stack.push(i);
181+
}
182+
res
183+
}
184+
}
185+
```
186+
142187
### **...**
143188

144189
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
3+
let n = temperatures.len();
4+
let mut stack = Vec::new();
5+
let mut res = vec![0; n];
6+
for i in (0..n).rev() {
7+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] <= temperatures[i] {
8+
stack.pop();
9+
}
10+
res[i] = if stack.is_empty() {
11+
0
12+
} else {
13+
(stack.last().unwrap() - i) as i32
14+
};
15+
stack.push(i);
16+
}
17+
res
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function dailyTemperatures(temperatures: number[]): number[] {
2+
const n = temperatures.length;
3+
const stack = [];
4+
const res = new Array(n);
5+
for (let i = n - 1; i >= 0; i--) {
6+
while (
7+
stack.length !== 0 &&
8+
temperatures[stack[stack.length - 1]] <= temperatures[i]
9+
) {
10+
stack.pop();
11+
}
12+
res[i] = stack.length === 0 ? 0 : stack[stack.length - 1] - i;
13+
stack.push(i);
14+
}
15+
return res;
16+
}

0 commit comments

Comments
 (0)