Skip to content

Commit 192ccef

Browse files
authored
feat: add swift implementation to lcof2 problem: No.035 (doocs#3040)
1 parent d2d77aa commit 192ccef

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lcof2/剑指 Offer II 035. 最小时间差/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,35 @@ function findMinDifference(timePoints: string[]): number {
167167
}
168168
```
169169

170+
#### Swift
171+
172+
```swift
173+
class Solution {
174+
func findMinDifference(_ timePoints: [String]) -> Int {
175+
if timePoints.count > 24 * 60 {
176+
return 0
177+
}
178+
179+
var mins = [Int]()
180+
181+
for t in timePoints {
182+
let time = t.split(separator: ":").map { Int($0)! }
183+
mins.append(time[0] * 60 + time[1])
184+
}
185+
186+
mins.sort()
187+
mins.append(mins[0] + 24 * 60)
188+
189+
var ans = Int.max
190+
for i in 1..<mins.count {
191+
ans = min(ans, mins[i] - mins[i - 1])
192+
}
193+
194+
return ans
195+
}
196+
}
197+
```
198+
170199
<!-- tabs:end -->
171200

172201
<!-- solution:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func findMinDifference(_ timePoints: [String]) -> Int {
3+
if timePoints.count > 24 * 60 {
4+
return 0
5+
}
6+
7+
var mins = [Int]()
8+
9+
for t in timePoints {
10+
let time = t.split(separator: ":").map { Int($0)! }
11+
mins.append(time[0] * 60 + time[1])
12+
}
13+
14+
mins.sort()
15+
mins.append(mins[0] + 24 * 60)
16+
17+
var ans = Int.max
18+
for i in 1..<mins.count {
19+
ans = min(ans, mins[i] - mins[i - 1])
20+
}
21+
22+
return ans
23+
}
24+
}

0 commit comments

Comments
 (0)