Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf2b016d

Browse files
refactor 539
1 parent9ac4c91 commitf2b016d

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

‎src/main/java/com/fishercoder/solutions/_539.java

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
importjava.util.List;
44

55
/**
6-
* Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.
6+
* 539. Minimum Time Difference
7+
*
8+
* Given a list of 24-hour clock time points in "Hour:Minutes" format,
9+
* find the minimum minutes difference between any two time points in the list.
710
811
Example 1:
912
Input: ["23:59","00:00"]
@@ -15,37 +18,40 @@
1518
*/
1619
publicclass_539 {
1720

18-
publicintfindMinDifference(List<String>timePoints) {
19-
// there are in total 24*60 = 1440 possible time points
20-
finalintALL_POSSIBLE_TIMEPOINTS =1440;
21-
boolean[]allTimePoints =newboolean[ALL_POSSIBLE_TIMEPOINTS];
22-
for (StringeachTime :timePoints) {
23-
String[]timeParts =eachTime.split(":");
24-
inthour =Integer.valueOf(timeParts[0]);
25-
intminute =Integer.valueOf(timeParts[1]);
26-
intvalue =hour *60 +minute;
27-
if (allTimePoints[value]) {
28-
return0;
21+
publicstaticclassSoluiton1 {
22+
23+
publicintfindMinDifference(List<String>timePoints) {
24+
/**there are in total 24*60 = 1440 possible time points*/
25+
finalintALL_POSSIBLE_TIMEPOINTS =1440;
26+
boolean[]allTimePoints =newboolean[ALL_POSSIBLE_TIMEPOINTS];
27+
for (StringeachTime :timePoints) {
28+
String[]timeParts =eachTime.split(":");
29+
inthour =Integer.valueOf(timeParts[0]);
30+
intminute =Integer.valueOf(timeParts[1]);
31+
intvalue =hour *60 +minute;
32+
if (allTimePoints[value]) {
33+
return0;
34+
}
35+
allTimePoints[value] =true;
2936
}
30-
allTimePoints[value] =true;
31-
}
3237

33-
intmin =Integer.MAX_VALUE;
34-
intprev =0;
35-
intfirst =Integer.MAX_VALUE;
36-
intlast =Integer.MIN_VALUE;
37-
for (inti =0;i <ALL_POSSIBLE_TIMEPOINTS;i++) {
38-
if (allTimePoints[i]) {
39-
if (first !=Integer.MAX_VALUE) {
40-
min =Math.min(min,i -prev);
38+
intmin =Integer.MAX_VALUE;
39+
intprev =0;
40+
intfirst =Integer.MAX_VALUE;
41+
intlast =Integer.MIN_VALUE;
42+
for (inti =0;i <ALL_POSSIBLE_TIMEPOINTS;i++) {
43+
if (allTimePoints[i]) {
44+
if (first !=Integer.MAX_VALUE) {
45+
min =Math.min(min,i -prev);
46+
}
47+
first =Math.min(first,i);
48+
last =Math.max(last,i);
49+
prev =i;
4150
}
42-
first =Math.min(first,i);
43-
last =Math.max(last,i);
44-
prev =i;
4551
}
52+
min =Math.min(min, (ALL_POSSIBLE_TIMEPOINTS -last +first));
53+
returnmin;
4654
}
47-
min =Math.min(min, (ALL_POSSIBLE_TIMEPOINTS -last +first));
48-
returnmin;
4955
}
5056

5157
}

‎src/test/java/com/fishercoder/_539Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
importstaticjunit.framework.Assert.assertEquals;
1313

1414
publicclass_539Test {
15-
privatestatic_539test;
15+
privatestatic_539.Soluiton1soluiton1;
1616
privatestaticintexpected;
1717
privatestaticintactual;
1818
privatestaticList<String>timePoints;
1919

2020
@BeforeClass
2121
publicstaticvoidsetup() {
22-
test =new_539();
22+
soluiton1 =new_539.Soluiton1();
2323
}
2424

2525
@Before
@@ -32,15 +32,15 @@ public void setupForEachTest() {
3232
publicvoidtest1() {
3333
timePoints =newArrayList<>(Arrays.asList("23:59","00:00"));
3434
expected =1;
35-
actual =test.findMinDifference(timePoints);
35+
actual =soluiton1.findMinDifference(timePoints);
3636
assertEquals(expected,actual);
3737
}
3838

3939
@Test
4040
publicvoidtest2() {
4141
timePoints =newArrayList<>(Arrays.asList("23:59","00:00","01:20"));
4242
expected =1;
43-
actual =test.findMinDifference(timePoints);
43+
actual =soluiton1.findMinDifference(timePoints);
4444
assertEquals(expected,actual);
4545
}
4646

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp