Skip to content

feat: add swift implementation to lcof2 problem: No.038 #3048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add swift implementation to lcof2 problem: No.038
  • Loading branch information
klever34 committed Jun 6, 2024
commit 7e17932c957e860efb954a33ace0b00e03f1e3d9
22 changes: 22 additions & 0 deletions lcof2/剑指 Offer II 038. 每日温度/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ impl Solution {
}
```

#### Swift

```swift
class Solution {
func dailyTemperatures(_ temperatures: [Int]) -> [Int] {
let n = temperatures.count
var ans = [Int](repeating: 0, count: n)
var stack = [Int]()

for i in 0..<n {
while !stack.isEmpty && temperatures[stack.last!] < temperatures[i] {
let j = stack.removeLast()
ans[j] = i - j
}
stack.append(i)
}

return ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
17 changes: 17 additions & 0 deletions lcof2/剑指 Offer II 038. 每日温度/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
func dailyTemperatures(_ temperatures: [Int]) -> [Int] {
let n = temperatures.count
var ans = [Int](repeating: 0, count: n)
var stack = [Int]()

for i in 0..<n {
while !stack.isEmpty && temperatures[stack.last!] < temperatures[i] {
let j = stack.removeLast()
ans[j] = i - j
}
stack.append(i)
}

return ans
}
}