Closed congr closed 5 years ago
Time : NLogN Space : 3N
class Solution {
public String[] reorderLogFiles(String[] logs) {
List<String> list = new LinkedList();
List<String> digits = new LinkedList();
TreeMap<String, String> map = new TreeMap();
for (String s : logs) {
int space = s.indexOf(" ");
String rest = s.substring(space+1, s.length());
if (rest.charAt(0) >= '0' && rest.charAt(0) <= '9') digits.add(s);
else map.put(rest, s);
}
for (String s : map.values()) list.add(s);
int i = 0;
String[] res = new String[logs.length];
for (String s : list) res[i++] = s;
for (String s : digits) res[i++] = s;
return res;
}
}
class Solution {
public String[] reorderLogFiles(String[] logs) {
Arrays.sort(logs, (a,b) -> {
String a1 = a.substring(a.indexOf(" ")+1, a.length());
String b1 = b.substring(b.indexOf(" ")+1, b.length());
boolean isDigit1 = Character.isDigit(a1.charAt(0));
boolean isDigit2 = Character.isDigit(b1.charAt(0));
if (isDigit1 && isDigit2) return 0;
else if (isDigit1) return 1;
else if (isDigit2) return -1;
else return a1.compareTo(b1);
});
return logs;
}
}
https://leetcode.com/problems/reorder-log-files/