|
4 | 4 | importjava.util.List;
|
5 | 5 | importjava.util.TreeMap;
|
6 | 6 |
|
7 |
| -/** |
8 |
| - * 937. Reorder Log Files |
9 |
| - * |
10 |
| - * You have an array of logs. Each log is a space delimited string of words. |
11 |
| - * |
12 |
| - * For each log, the first word in each log is an alphanumeric identifier. Then, either: |
13 |
| - * |
14 |
| - * Each word after the identifier will consist only of lowercase letters, or; |
15 |
| - * Each word after the identifier will consist only of digits. |
16 |
| - * We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier. |
17 |
| - * |
18 |
| - * Reorder the logs so that all of the letter-logs come before any digit-log. |
19 |
| - * The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. |
20 |
| - * The digit-logs should be put in their original order. |
21 |
| - * |
22 |
| - * Return the final order of the logs. |
23 |
| - * |
24 |
| - * Example 1: |
25 |
| - * |
26 |
| - * Input: ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"] |
27 |
| - * Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"] |
28 |
| - * |
29 |
| - * Note: |
30 |
| - * 0 <= logs.length <= 100 |
31 |
| - * 3 <= logs[i].length <= 100 |
32 |
| - * logs[i] is guaranteed to have an identifier, and a word after the identifier. |
33 |
| - */ |
34 | 7 | publicclass_937 {
|
35 |
| -publicstaticclassSolution1 { |
36 |
| -publicString[]reorderLogFiles(String[]logs) { |
37 |
| -TreeMap<String,String>letterLogMap =newTreeMap<>(); |
38 |
| -List<String>digitLogList =newArrayList<>(); |
39 |
| -for (Stringlog :logs) { |
40 |
| -intfirstSpaceIndex =log.indexOf(' '); |
41 |
| -Stringid =log.substring(0,firstSpaceIndex); |
42 |
| -if (Character.isAlphabetic(log.charAt(firstSpaceIndex +1))) { |
43 |
| -Stringkey =log.substring(firstSpaceIndex +1) +id; |
44 |
| -letterLogMap.put(key,log); |
45 |
| - }else { |
46 |
| -digitLogList.add(log); |
| 8 | +publicstaticclassSolution1 { |
| 9 | +publicString[]reorderLogFiles(String[]logs) { |
| 10 | +TreeMap<String,String>letterLogMap =newTreeMap<>(); |
| 11 | +List<String>digitLogList =newArrayList<>(); |
| 12 | +for (Stringlog :logs) { |
| 13 | +intfirstSpaceIndex =log.indexOf(' '); |
| 14 | +Stringid =log.substring(0,firstSpaceIndex); |
| 15 | +if (Character.isAlphabetic(log.charAt(firstSpaceIndex +1))) { |
| 16 | +Stringkey =log.substring(firstSpaceIndex +1) +id; |
| 17 | +letterLogMap.put(key,log); |
| 18 | + }else { |
| 19 | +digitLogList.add(log); |
| 20 | + } |
| 21 | + } |
| 22 | +String[]reorderedLogs =newString[logs.length]; |
| 23 | +inti =0; |
| 24 | +for (Stringkey :letterLogMap.keySet()) { |
| 25 | +reorderedLogs[i++] =letterLogMap.get(key); |
| 26 | + } |
| 27 | +for (Stringlog :digitLogList) { |
| 28 | +reorderedLogs[i++] =log; |
| 29 | + } |
| 30 | +returnreorderedLogs; |
47 | 31 | }
|
48 |
| - } |
49 |
| -String[]reorderedLogs =newString[logs.length]; |
50 |
| -inti =0; |
51 |
| -for (Stringkey :letterLogMap.keySet()) { |
52 |
| -reorderedLogs[i++] =letterLogMap.get(key); |
53 |
| - } |
54 |
| -for (Stringlog :digitLogList) { |
55 |
| -reorderedLogs[i++] =log; |
56 |
| - } |
57 |
| -returnreorderedLogs; |
58 | 32 | }
|
59 |
| - } |
60 | 33 | }
|