Skip to content

Commit 6b390eb

Browse files
committed
feat: add solutions to lc problem: No.0937
No.0937.Reorder Data in Log Files
1 parent d3fb003 commit 6b390eb

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

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

+50
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,56 @@
7777

7878
```
7979

80+
### **TypeScript**
81+
82+
```ts
83+
function reorderLogFiles(logs: string[]): string[] {
84+
const isDigit = (c: string) => c >= '0' && c <= '9';
85+
return logs.sort((a, b) => {
86+
const end1 = a[a.length - 1];
87+
const end2 = b[b.length - 1];
88+
if (isDigit(end1) && isDigit(end2)) {
89+
return 0;
90+
}
91+
if (isDigit(end1)) {
92+
return 1;
93+
}
94+
if (isDigit(end2)) {
95+
return -1;
96+
}
97+
const content1 = a.split(' ').slice(1).join(' ');
98+
const content2 = b.split(' ').slice(1).join(' ');
99+
if (content1 === content2) {
100+
return a < b ? -1 : 1;
101+
}
102+
return content1 < content2 ? -1 : 1;
103+
});
104+
}
105+
```
106+
107+
### **Rust**
108+
109+
```rust
110+
impl Solution {
111+
pub fn reorder_log_files(mut logs: Vec<String>) -> Vec<String> {
112+
logs.sort_by(|s1, s2| {
113+
let (start1, content1) = s1.split_once(' ').unwrap();
114+
let (start2, content2) = s2.split_once(' ').unwrap();
115+
match (
116+
content1.chars().nth(0).unwrap().is_digit(10),
117+
content2.chars().nth(0).unwrap().is_digit(10),
118+
) {
119+
(true, true) => std::cmp::Ordering::Equal,
120+
(true, false) => std::cmp::Ordering::Greater,
121+
(false, true) => std::cmp::Ordering::Less,
122+
(false, false) => content1.cmp(&content2).then(start1.cmp(&start2)),
123+
}
124+
});
125+
logs
126+
}
127+
}
128+
```
129+
80130
### **...**
81131

82132
```

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

+50
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,56 @@ The digit-logs have a relative order of &quot;dig1 8 1 5 1&quot;, &quot;dig2 3 6
6767

6868
```
6969

70+
### **TypeScript**
71+
72+
```ts
73+
function reorderLogFiles(logs: string[]): string[] {
74+
const isDigit = (c: string) => c >= '0' && c <= '9';
75+
return logs.sort((a, b) => {
76+
const end1 = a[a.length - 1];
77+
const end2 = b[b.length - 1];
78+
if (isDigit(end1) && isDigit(end2)) {
79+
return 0;
80+
}
81+
if (isDigit(end1)) {
82+
return 1;
83+
}
84+
if (isDigit(end2)) {
85+
return -1;
86+
}
87+
const content1 = a.split(' ').slice(1).join(' ');
88+
const content2 = b.split(' ').slice(1).join(' ');
89+
if (content1 === content2) {
90+
return a < b ? -1 : 1;
91+
}
92+
return content1 < content2 ? -1 : 1;
93+
});
94+
}
95+
```
96+
97+
### **Rust**
98+
99+
```rust
100+
impl Solution {
101+
pub fn reorder_log_files(mut logs: Vec<String>) -> Vec<String> {
102+
logs.sort_by(|s1, s2| {
103+
let (start1, content1) = s1.split_once(' ').unwrap();
104+
let (start2, content2) = s2.split_once(' ').unwrap();
105+
match (
106+
content1.chars().nth(0).unwrap().is_digit(10),
107+
content2.chars().nth(0).unwrap().is_digit(10),
108+
) {
109+
(true, true) => std::cmp::Ordering::Equal,
110+
(true, false) => std::cmp::Ordering::Greater,
111+
(false, true) => std::cmp::Ordering::Less,
112+
(false, false) => content1.cmp(&content2).then(start1.cmp(&start2)),
113+
}
114+
});
115+
logs
116+
}
117+
}
118+
```
119+
70120
### **...**
71121

72122
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn reorder_log_files(mut logs: Vec<String>) -> Vec<String> {
3+
logs.sort_by(|s1, s2| {
4+
let (start1, content1) = s1.split_once(' ').unwrap();
5+
let (start2, content2) = s2.split_once(' ').unwrap();
6+
match (
7+
content1.chars().nth(0).unwrap().is_digit(10),
8+
content2.chars().nth(0).unwrap().is_digit(10),
9+
) {
10+
(true, true) => std::cmp::Ordering::Equal,
11+
(true, false) => std::cmp::Ordering::Greater,
12+
(false, true) => std::cmp::Ordering::Less,
13+
(false, false) => content1.cmp(&content2).then(start1.cmp(&start2)),
14+
}
15+
});
16+
logs
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function reorderLogFiles(logs: string[]): string[] {
2+
const isDigit = (c: string) => c >= '0' && c <= '9';
3+
return logs.sort((a, b) => {
4+
const end1 = a[a.length - 1];
5+
const end2 = b[b.length - 1];
6+
if (isDigit(end1) && isDigit(end2)) {
7+
return 0;
8+
}
9+
if (isDigit(end1)) {
10+
return 1;
11+
}
12+
if (isDigit(end2)) {
13+
return -1;
14+
}
15+
const content1 = a.split(' ').slice(1).join(' ');
16+
const content2 = b.split(' ').slice(1).join(' ');
17+
if (content1 === content2) {
18+
return a < b ? -1 : 1;
19+
}
20+
return content1 < content2 ? -1 : 1;
21+
});
22+
}

0 commit comments

Comments
 (0)