@@ -50,11 +50,11 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof2/%E5%89%91%E6%8C%87%2
50
50
51
51
### 方法一:排序
52
52
53
- 我们注意到,时间点最多只有 $24 \times 60$ 个,因此,当 $timePoints$ 长度超过 $24 \times 60 $,说明有重复的时间点,提前返回 $0$。
53
+ 我们注意到,时间点最多只有 $24 \times 60 = 1440 $ 个,因此,当 $timePoints$ 长度超过 $1440 $,说明有重复的时间点,提前返回 $0$。
54
54
55
- 接下来,我们首先遍历时间列表,将其转换为“分钟制”列表 $mins $,比如,对于时间点 ` 13:14 ` ,将其转换为 $13 \times 60 + 14$。
55
+ 接下来,我们首先遍历时间列表,将其转换为“分钟制”列表 $nums $,比如,对于时间点 ` 13:14 ` ,将其转换为 $13 \times 60 + 14$。
56
56
57
- 接着将“分钟制”列表按升序排列,然后将此列表的最小时间 $mins [ 0] $ 加上 $24 \times 60 $ 追加至列表尾部,用于处理最大值、最小值的差值这种特殊情况。
57
+ 接着将“分钟制”列表按升序排列,然后将此列表的最小时间 $nums [ 0] $ 加上 $1440 $ 追加至列表尾部,用于处理最大值、最小值的差值这种特殊情况。
58
58
59
59
最后遍历“分钟制”列表,找出相邻两个时间的最小值即可。
60
60
@@ -67,31 +67,32 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof2/%E5%89%91%E6%8C%87%2
67
67
``` python
68
68
class Solution :
69
69
def findMinDifference (self , timePoints : List[str ]) -> int :
70
- if len (timePoints) > 24 * 60 :
70
+ if len (timePoints) > 1440 :
71
71
return 0
72
- mins = sorted (int (t [:2 ]) * 60 + int (t [3 :]) for t in timePoints)
73
- mins .append(mins [0 ] + 24 * 60 )
74
- return min (b - a for a, b in pairwise(mins ))
72
+ nums = sorted (int (x [:2 ]) * 60 + int (x [3 :]) for x in timePoints)
73
+ nums .append(nums [0 ] + 1440 )
74
+ return min (b - a for a, b in pairwise(nums ))
75
75
```
76
76
77
77
#### Java
78
78
79
79
``` java
80
80
class Solution {
81
81
public int findMinDifference (List<String > timePoints ) {
82
- if (timePoints. size() > 24 * 60 ) {
82
+ if (timePoints. size() > 1440 ) {
83
83
return 0 ;
84
84
}
85
- List<Integer > mins = new ArrayList<> ();
86
- for (String t : timePoints) {
87
- String [] time = t. split(" :" );
88
- mins. add(Integer . parseInt(time[0 ]) * 60 + Integer . parseInt(time[1 ]));
85
+ int n = timePoints. size();
86
+ int [] nums = new int [n + 1 ];
87
+ for (int i = 0 ; i < n; ++ i) {
88
+ String [] t = timePoints. get(i). split(" :" );
89
+ nums[i] = Integer . parseInt(t[0 ]) * 60 + Integer . parseInt(t[1 ]);
89
90
}
90
- Collections . sort(mins );
91
- mins . add(mins . get( 0 ) + 24 * 60 ) ;
91
+ Arrays . sort(nums, 0 , n );
92
+ nums[n] = nums[ 0 ] + 1440 ;
92
93
int ans = 1 << 30 ;
93
- for (int i = 1 ; i < mins . size() ; ++ i) {
94
- ans = Math . min(ans, mins . get(i) - mins . get( i - 1 ) );
94
+ for (int i = 1 ; i <= n ; ++ i) {
95
+ ans = Math . min(ans, nums[i] - nums[ i - 1 ] );
95
96
}
96
97
return ans;
97
98
}
@@ -104,18 +105,21 @@ class Solution {
104
105
class Solution {
105
106
public:
106
107
int findMinDifference(vector<string >& timePoints) {
107
- if (timePoints.size() > 24 * 60 ) {
108
+ if (timePoints.size() > 1440 ) {
108
109
return 0;
109
110
}
110
- vector<int > mins;
111
- for (auto& t : timePoints) {
112
- mins.push_back(stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3)));
111
+ int n = timePoints.size();
112
+ vector<int > nums(n + 1);
113
+ for (int i = 0; i < n; ++i) {
114
+ int hours = stoi(timePoints[ i] .substr(0, 2));
115
+ int minutes = stoi(timePoints[ i] .substr(3, 2));
116
+ nums[ i] = hours * 60 + minutes;
113
117
}
114
- sort(mins .begin(), mins.end() );
115
- mins.push_back(mins [ 0] + 24 * 60) ;
116
- int ans = 1 << 30 ;
117
- for (int i = 1; i < mins.size() ; ++i) {
118
- ans = min(ans, mins [ i] - mins [ i - 1] );
118
+ sort(nums .begin(), nums.begin() + n );
119
+ nums [ n ] = nums [ 0] + 1440 ;
120
+ int ans = INT_MAX ;
121
+ for (int i = 1; i <= n ; ++i) {
122
+ ans = min(ans, nums [ i] - nums [ i - 1] );
119
123
}
120
124
return ans;
121
125
}
@@ -126,22 +130,27 @@ public:
126
130
127
131
```go
128
132
func findMinDifference(timePoints []string) int {
129
- if len(timePoints) > 24*60 {
133
+ if len(timePoints) > 1440 {
130
134
return 0
131
135
}
132
- var mins []int
133
- for _, t := range timePoints {
134
- time := strings.Split(t, ":")
135
- h, _ := strconv.Atoi(time[0])
136
- m, _ := strconv.Atoi(time[1])
137
- mins = append(mins, h*60+m)
136
+
137
+ n := len(timePoints)
138
+ nums := make([]int, n+1)
139
+ for i, time := range timePoints {
140
+ parts := strings.Split(time, ":")
141
+ hours, _ := strconv.Atoi(parts[0])
142
+ minutes, _ := strconv.Atoi(parts[1])
143
+ nums[i] = hours*60 + minutes
138
144
}
139
- sort.Ints(mins)
140
- mins = append(mins, mins[0]+24*60)
145
+
146
+ sort.Ints(nums[:n])
147
+ nums[n] = nums[0] + 1440
148
+
141
149
ans := 1 << 30
142
- for i, x := range mins[1:] {
143
- ans = min(ans, x-mins[i ])
150
+ for i := 1; i <= n; i++ {
151
+ ans = min(ans, nums[i]-nums[i-1 ])
144
152
}
153
+
145
154
return ans
146
155
}
147
156
```
@@ -150,45 +159,78 @@ func findMinDifference(timePoints []string) int {
150
159
151
160
``` ts
152
161
function findMinDifference(timePoints : string []): number {
153
- if (timePoints .length > 24 * 60 ) {
162
+ if (timePoints .length > 1440 ) {
154
163
return 0 ;
155
164
}
156
- const mins: number [] = timePoints .map (timePoint => {
157
- const [hour, minute] = timePoint .split (' :' ).map (num => parseInt (num ));
158
- return hour * 60 + minute ;
159
- });
160
- mins .sort ((a , b ) => a - b );
161
- mins .push (mins [0 ] + 24 * 60 );
165
+ const n = timePoints .length ;
166
+ const nums: number [] = Array (n + 1 );
167
+ for (let i = 0 ; i < n ; ++ i ) {
168
+ const [hours, minutes] = timePoints [i ].split (' :' ).map (Number );
169
+ nums [i ] = hours * 60 + minutes ;
170
+ }
171
+ nums .sort ((a , b ) => a - b );
172
+ nums [n ] = nums [0 ] + 1440 ;
162
173
let ans = 1 << 30 ;
163
- for (let i = 1 ; i < mins . length ; ++ i ) {
164
- ans = Math .min (ans , mins [i ] - mins [i - 1 ]);
174
+ for (let i = 1 ; i <= n ; ++ i ) {
175
+ ans = Math .min (ans , nums [i ] - nums [i - 1 ]);
165
176
}
166
177
return ans ;
167
178
}
168
179
```
169
180
181
+ #### Rust
182
+
183
+ ``` rust
184
+ impl Solution {
185
+ pub fn find_min_difference (time_points : Vec <String >) -> i32 {
186
+ if time_points . len () > 1440 {
187
+ return 0 ;
188
+ }
189
+
190
+ let n = time_points . len ();
191
+ let mut nums : Vec <i32 > = Vec :: with_capacity (n + 1 );
192
+
193
+ for time in time_points . iter () {
194
+ let parts : Vec <i32 > = time . split (':' ). map (| s | s . parse (). unwrap ()). collect ();
195
+ let minutes = parts [0 ] * 60 + parts [1 ];
196
+ nums . push (minutes );
197
+ }
198
+
199
+ nums . sort ();
200
+ nums . push (nums [0 ] + 1440 );
201
+
202
+ let mut ans = i32 :: MAX ;
203
+ for i in 1 ..= n {
204
+ ans = ans . min (nums [i ] - nums [i - 1 ]);
205
+ }
206
+
207
+ ans
208
+ }
209
+ }
210
+ ```
211
+
170
212
#### Swift
171
213
172
214
``` swift
173
215
class Solution {
174
216
func findMinDifference (_ timePoints : [String ]) -> Int {
175
- if timePoints.count > 24 * 60 {
217
+ if timePoints.count > 1440 {
176
218
return 0
177
219
}
178
220
179
- var mins = [Int ]()
221
+ var nums = [Int ]()
180
222
181
223
for t in timePoints {
182
224
let time = t.split (separator : " :" ).map { Int ($0 )! }
183
- mins .append (time[0 ] * 60 + time[1 ])
225
+ nums .append (time[0 ] * 60 + time[1 ])
184
226
}
185
227
186
- mins .sort ()
187
- mins .append (mins [0 ] + 24 * 60 )
228
+ nums .sort ()
229
+ nums .append (nums [0 ] + 1440 )
188
230
189
231
var ans = Int .max
190
- for i in 1 ..< mins .count {
191
- ans = min (ans, mins [i] - mins [i - 1 ])
232
+ for i in 1 ..< nums .count {
233
+ ans = min (ans, nums [i] - nums [i - 1 ])
192
234
}
193
235
194
236
return ans
0 commit comments