-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
937.java
66 lines (63 loc) · 2.46 KB
/
937.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public String[] reorderLogFiles(String[] logs) {
List<String> orderedLogs = new ArrayList<>();
List<String> noIdentifierLetterLogs = new ArrayList<>();
int letterLogCount = 0;
for(int i = 0; i < logs.length; i++){
int j = logs[i].indexOf(' ');
if(Character.isDigit(logs[i].charAt(j + 1))) {
orderedLogs.add(logs[i]);
continue;
}
String next = logs[i].substring(j + 1);
int index = 0;
// compare no identifier logs
while((index < letterLogCount) && (next.compareTo(noIdentifierLetterLogs.get(index)) > 0)){
++index;
}
// if no identifier logs are equal, compare using full log string
if(index < letterLogCount && next.compareTo(noIdentifierLetterLogs.get(index)) == 0){
if(logs[i].compareTo(orderedLogs.get(index)) > 0)
++index;
}
orderedLogs.add(index, logs[i]);
noIdentifierLetterLogs.add(index, next);
++letterLogCount;
}
return orderedLogs.toArray(new String[orderedLogs.size()]);
}
}
__________________________________________________________________________________________________
sample 35740 kb submission
class Solution {
public String[] reorderLogFiles(String[] logs) {
Arrays.sort(logs, (a,b)->{
int index1 = a.indexOf(" ");
int index2 = b.indexOf(" ");
char c1 = a.charAt(index1+1);
char c2 = b.charAt(index2+1);
if (isLetter(c1) && isLetter(c2)) {
String sub1 = a.substring(index1+1);
String sub2 = b.substring(index2+1);
if (!sub1.equals(sub2))
return sub1.compareTo(sub2);
return a.compareTo(b);
} else if (!isLetter(c1) && isLetter(c2)) {
return 1;
} else if (isLetter(c1) && !isLetter(c2)) {
return -1;
} else {
return 0;
}
});
return logs;
}
private boolean isLetter(char c) {
if (c >= 'a' && c <= 'z')
return true;
return false;
}
}
__________________________________________________________________________________________________