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

Commit362aca7

Browse files
refactor 937
1 parentdd75347 commit362aca7

File tree

1 file changed

+23
-50
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-50
lines changed

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

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,30 @@
44
importjava.util.List;
55
importjava.util.TreeMap;
66

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-
*/
347
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;
4731
}
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;
5832
}
59-
}
6033
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp