Skip to content

Commit e7cf47c

Browse files
committed
feat: add solutions to lc problem: No.0937
No.0937.Reorder Data in Log Files
1 parent 832a5a3 commit e7cf47c

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

solution/0900-0999/0937.Reorder Data in Log Files/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,50 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:自定义排序**
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
<!-- 这里可写当前语言的特殊实现逻辑 -->
6769

6870
```python
71+
class Solution:
72+
def reorderLogFiles(self, logs: List[str]) -> List[str]:
73+
def cmp(x):
74+
a, b = x.split(' ', 1)
75+
return (0, b, a) if b[0].isalpha() else (1,)
6976

77+
return sorted(logs, key=cmp)
7078
```
7179

7280
### **Java**
7381

7482
<!-- 这里可写当前语言的特殊实现逻辑 -->
7583

7684
```java
85+
class Solution {
86+
public String[] reorderLogFiles(String[] logs) {
87+
Arrays.sort(logs, this::cmp);
88+
return logs;
89+
}
7790

91+
private int cmp(String a, String b) {
92+
String[] t1 = a.split(" ", 2);
93+
String[] t2 = b.split(" ", 2);
94+
boolean d1 = Character.isDigit(t1[1].charAt(0));
95+
boolean d2 = Character.isDigit(t2[1].charAt(0));
96+
if (!d1 && !d2) {
97+
int v = t1[1].compareTo(t2[1]);
98+
return v == 0 ? t1[0].compareTo(t2[0]) : v;
99+
}
100+
if (d1 && d2) {
101+
return 0;
102+
}
103+
return d1 ? 1 : -1;
104+
}
105+
}
78106
```
79107

80108
### **TypeScript**

solution/0900-0999/0937.Reorder Data in Log Files/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,39 @@ The digit-logs have a relative order of &quot;dig1 8 1 5 1&quot;, &quot;dig2 3 6
5858
### **Python3**
5959

6060
```python
61+
class Solution:
62+
def reorderLogFiles(self, logs: List[str]) -> List[str]:
63+
def cmp(x):
64+
a, b = x.split(' ', 1)
65+
return (0, b, a) if b[0].isalpha() else (1,)
6166

67+
return sorted(logs, key=cmp)
6268
```
6369

6470
### **Java**
6571

6672
```java
73+
class Solution {
74+
public String[] reorderLogFiles(String[] logs) {
75+
Arrays.sort(logs, this::cmp);
76+
return logs;
77+
}
6778

79+
private int cmp(String a, String b) {
80+
String[] t1 = a.split(" ", 2);
81+
String[] t2 = b.split(" ", 2);
82+
boolean d1 = Character.isDigit(t1[1].charAt(0));
83+
boolean d2 = Character.isDigit(t2[1].charAt(0));
84+
if (!d1 && !d2) {
85+
int v = t1[1].compareTo(t2[1]);
86+
return v == 0 ? t1[0].compareTo(t2[0]) : v;
87+
}
88+
if (d1 && d2) {
89+
return 0;
90+
}
91+
return d1 ? 1 : -1;
92+
}
93+
}
6894
```
6995

7096
### **TypeScript**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public String[] reorderLogFiles(String[] logs) {
3+
Arrays.sort(logs, this::cmp);
4+
return logs;
5+
}
6+
7+
private int cmp(String a, String b) {
8+
String[] t1 = a.split(" ", 2);
9+
String[] t2 = b.split(" ", 2);
10+
boolean d1 = Character.isDigit(t1[1].charAt(0));
11+
boolean d2 = Character.isDigit(t2[1].charAt(0));
12+
if (!d1 && !d2) {
13+
int v = t1[1].compareTo(t2[1]);
14+
return v == 0 ? t1[0].compareTo(t2[0]) : v;
15+
}
16+
if (d1 && d2) {
17+
return 0;
18+
}
19+
return d1 ? 1 : -1;
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def reorderLogFiles(self, logs: List[str]) -> List[str]:
3+
def cmp(x):
4+
a, b = x.split(' ', 1)
5+
return (0, b, a) if b[0].isalpha() else (1,)
6+
7+
return sorted(logs, key=cmp)

0 commit comments

Comments
 (0)