Skip to content

Commit e669e5e

Browse files
committed
fd
1 parent 9aaf89a commit e669e5e

File tree

4 files changed

+3
-55
lines changed

4 files changed

+3
-55
lines changed

doc/Attention.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ return -1;
9696
不能直接写return o1 > o2 ? 1 : -1;
9797
因为可能存在重复的数,在TreeMap中查找key时通过comparator等于0,上述写法导致永远找不到key。
9898

99-
三十,StringBuilder的insert(0, c)不要频繁调用,不如append,最后reverse。
99+
三十,StringBuilder的insert(0, c)不要频繁调用,看源码要array copy,性能很差。不如append,最后reverse。
100100

101101
三十一,List在allAll一个PriorityQueue时,是不会带顺序的,正确的做法是while(!queue.isEmpty()) {list.add(queue.poll());}

leetcode/solution/src/LicenseKeyFormatting.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
* 将字符串s按长度k为一组,从右往左重排,组之间用"-"分隔,还要转成大写
33
* 注意别在最前面多加一个"-",即下面判断i != 0
44
*/
5-
6-
75
public class LicenseKeyFormatting {
86

9-
// 耗时22ms
7+
// 耗时13ms
108
public String licenseKeyFormatting(String S, int K) {
119
StringBuilder sb = new StringBuilder();
1210

1311
for (int i = S.length() - 1, j = 0; i >= 0; i--) {
1412
if (S.charAt(i) != '-') {
1513
if (j % K == 0 && sb.length() > 0) {
1614
sb.append("-");
17-
j = 0;
1815
}
1916

2017
sb.append(Character.toUpperCase(S.charAt(i)));

leetcode/solution/src/MeetingRoomsII.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class MeetingRoomsII {
77

8-
// 耗时17ms,时间复杂度O(nlgn)
8+
// 耗时7ms,时间复杂度O(nlgn)
99
public int minMeetingRooms(Interval[] intervals) {
1010
Arrays.sort(intervals, new Comparator<Interval>() {
1111
@Override

leetcode/src/Main.java

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,6 @@
22

33
public class Main {
44

5-
public String nextClosestTime(String time) {
6-
int[] number = new int[4];
7-
8-
time = time.substring(0, 2) + time.substring(3);
9-
10-
for (int i = 0; i < time.length(); i++) {
11-
number[i] = time.charAt(i) - '0';
12-
}
13-
14-
Arrays.sort(number);
15-
16-
StringBuilder sb = new StringBuilder(time);
17-
18-
int i;
19-
for (i = 3; i >= 0; i--) {
20-
char c = time.charAt(i);
21-
int k = nextNumber(number, c - '0');
22-
if (k >= 0) {
23-
sb.setCharAt(i, (char) (k + '0'));
24-
if (isValid(sb)) {
25-
break;
26-
}
27-
sb.setCharAt(i, c);
28-
}
29-
}
30-
for (i++; i < 4; i++) {
31-
sb.setCharAt(i, (char) (number[0] + '0'));
32-
}
33-
sb.insert(2, ':');
34-
return sb.toString();
35-
}
36-
37-
private boolean isValid(StringBuilder sb) {
38-
boolean flag1 = sb.charAt(0) < '2' || (sb.charAt(0) == '2' && sb.charAt(1) <= '3');
39-
boolean flag2 = sb.charAt(2) < '5' || (sb.charAt(2) == '5' && sb.charAt(3) <= '9');
40-
return flag1 && flag2;
41-
}
42-
43-
private int nextNumber(int[] number, int n) {
44-
for (int i = 0; i < number.length; i++) {
45-
if (number[i] > n) {
46-
return number[i];
47-
}
48-
}
49-
return -1;
50-
}
51-
525
public static void main(String[] args) {
53-
String s = new NextClosestTime().nextClosestTime("13:55");
54-
System.out.println(s);
556
}
567
}

0 commit comments

Comments
 (0)