Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23858da

Browse files
feat: add swift implementation to lcof2 problem: No.038 (doocs#3048)
1 parent 5f7fde0 commit 23858da

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ impl Solution {
180180
}
181181
```
182182

183+
#### Swift
184+
185+
```swift
186+
class Solution {
187+
func dailyTemperatures(_ temperatures: [Int]) -> [Int] {
188+
let n = temperatures.count
189+
var ans = [Int](repeating: 0, count: n)
190+
var stack = [Int]()
191+
192+
for i in 0..<n {
193+
while !stack.isEmpty && temperatures[stack.last!] < temperatures[i] {
194+
let j = stack.removeLast()
195+
ans[j] = i - j
196+
}
197+
stack.append(i)
198+
}
199+
200+
return ans
201+
}
202+
}
203+
```
204+
183205
<!-- tabs:end -->
184206

185207
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
func dailyTemperatures(_ temperatures: [Int]) -> [Int] {
3+
let n = temperatures.count
4+
var ans = [Int](repeating: 0, count: n)
5+
var stack = [Int]()
6+
7+
for i in 0..<n {
8+
while !stack.isEmpty && temperatures[stack.last!] < temperatures[i] {
9+
let j = stack.removeLast()
10+
ans[j] = i - j
11+
}
12+
stack.append(i)
13+
}
14+
15+
return ans
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /