Skip to content

Commit db8d391

Browse files
committed
feat: update solutions to lcof2 problem: No.038
剑指 Offer II 038. 每日温度
1 parent c56fa13 commit db8d391

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

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

+48-2
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ func dailyTemperatures(temperatures []int) []int {
141141

142142
### **TypeScript**
143143

144+
倒序:
145+
144146
```ts
145147
function dailyTemperatures(temperatures: number[]): number[] {
146148
const n = temperatures.length;
147-
const stack = [];
148149
const res = new Array(n);
150+
const stack = [];
149151
for (let i = n - 1; i >= 0; i--) {
150152
while (
151153
stack.length !== 0 &&
@@ -160,14 +162,38 @@ function dailyTemperatures(temperatures: number[]): number[] {
160162
}
161163
```
162164

165+
正序:
166+
167+
```ts
168+
function dailyTemperatures(temperatures: number[]): number[] {
169+
const n = temperatures.length;
170+
const res = new Array(n).fill(0);
171+
const stack = [];
172+
for (let i = 0; i < n; i++) {
173+
const temperature = temperatures[i];
174+
while (
175+
stack.length !== 0 &&
176+
temperatures[stack[stack.length - 1]] < temperature
177+
) {
178+
const j = stack.pop();
179+
res[j] = i - j;
180+
}
181+
stack.push(i);
182+
}
183+
return res;
184+
}
185+
```
186+
163187
### **Rust**
164188

189+
倒序:
190+
165191
```rust
166192
impl Solution {
167193
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
168194
let n = temperatures.len();
169-
let mut stack = Vec::new();
170195
let mut res = vec![0; n];
196+
let mut stack = Vec::new();
171197
for i in (0..n).rev() {
172198
while !stack.is_empty() && temperatures[*stack.last().unwrap()] <= temperatures[i] {
173199
stack.pop();
@@ -184,6 +210,26 @@ impl Solution {
184210
}
185211
```
186212

213+
正序:
214+
215+
```rust
216+
impl Solution {
217+
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
218+
let n = temperatures.len();
219+
let mut res = vec![0; n];
220+
let mut stack = Vec::new();
221+
for i in 0..n {
222+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] < temperatures[i] {
223+
let j = stack.pop().unwrap();
224+
res[j] = (i - j) as i32;
225+
}
226+
stack.push(i);
227+
}
228+
res
229+
}
230+
}
231+
```
232+
187233
### **...**
188234

189235
```

lcof2/剑指 Offer II 038. 每日温度/Solution.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
impl Solution {
22
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
33
let n = temperatures.len();
4-
let mut stack = Vec::new();
54
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();
5+
let mut stack = Vec::new();
6+
for i in 0..n {
7+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] < temperatures[i] {
8+
let j = stack.pop().unwrap();
9+
res[j] = (i - j) as i32;
910
}
10-
res[i] = if stack.is_empty() {
11-
0
12-
} else {
13-
(stack.last().unwrap() - i) as i32
14-
};
1511
stack.push(i);
1612
}
1713
res

lcof2/剑指 Offer II 038. 每日温度/Solution.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
function dailyTemperatures(temperatures: number[]): number[] {
22
const n = temperatures.length;
3+
const res = new Array(n).fill(0);
34
const stack = [];
4-
const res = new Array(n);
5-
for (let i = n - 1; i >= 0; i--) {
5+
for (let i = 0; i < n; i++) {
6+
const temperature = temperatures[i];
67
while (
78
stack.length !== 0 &&
8-
temperatures[stack[stack.length - 1]] <= temperatures[i]
9+
temperatures[stack[stack.length - 1]] < temperature
910
) {
10-
stack.pop();
11+
const j = stack.pop();
12+
res[j] = i - j;
1113
}
12-
res[i] = stack.length === 0 ? 0 : stack[stack.length - 1] - i;
1314
stack.push(i);
1415
}
1516
return res;

0 commit comments

Comments
 (0)