File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
lcof2/剑指 Offer II 035. 最小时间差 Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,35 @@ function findMinDifference(timePoints: string[]): number {
167
167
}
168
168
```
169
169
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
+
170
199
<!-- tabs: end -->
171
200
172
201
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments