File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
lcof2/剑指 Offer II 038. 每日温度 Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -180,6 +180,28 @@ impl Solution {
180
180
}
181
181
```
182
182
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
+
183
205
<!-- tabs: end -->
184
206
185
207
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments