Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a9f9814

Browse files
committed
937_Reorder_Log_Files
1 parent 3fceeba commit a9f9814

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Also, there are open source implementations for basic data structs and algorithm
201201
| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/922_Sort_Array_By_Parity_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/922_Sort_Array_By_Parity_II.java) | 1. Place odd and even number in odd and even place, not sort is needed. O(n) and O(1)<br>2. Two points with quick sort swap idea, O(n) and O(1). |
202202
| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/929_Unique_Email_Addresses.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/929_Unique_Email_Addresses.java) | String handle and hash (or set) |
203203
| 933 | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/933_Number_of_Recent_Calls.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/933_Number_of_Recent_Calls.java) | Queue, remove val in head when val < t - 3000, O(n) and O(n) |
204+
| 937 | [Reorder Log Files](https://leetcode.com/problems/reorder-log-files/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/937_Reorder_Log_Files.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/937_Reorder_Log_Files.java) | 1. Comstom Sort, O(nlogn) and O(1)<br>2. Separete letter logs and digit logs, then sort letter logs and merge with digit logs, O(nlogn) and O(n) |
204205
| 945 | [Minimum Increment to Make Array Unique](https://leetcode.com/problems/minimum-increment-to-make-array-unique/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/945_Minimum_Increment_to_Make_Array_Unique.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/945_Minimum_Increment_to_Make_Array_Unique.java) | Sort, then list duplicate and missing value in sorted list. O(nlgn) and O(n) |
205206
| 946 | [Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/946_Validate_Stack_Sequences.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/946_Validate_Stack_Sequences.java) | Add a stack named inStack to help going through pushed and popped. O(n) and O(n) |
206207
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/contest/weekly-contest-114/problems/verifying-an-alien-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/953_Verifying_an_Alien_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/953_Verifying_an_Alien_Dictionary.java) | Use hashmap to store index of each value, then create a comparator based on this index, O(n) and O(n) |

java/937_Reorder_Log_Files.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.List;
2+
3+
class Solution {
4+
public String[] reorderLogFiles(String[] logs) {
5+
Arrays.sort(logs, (log1, log2) -> {
6+
String[] split1 = log1.split(" ", 2);
7+
String[] split2 = log2.split(" ", 2);
8+
boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
9+
boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
10+
if (!isDigit1 && !isDigit2) {
11+
int cmp = split1[1].compareTo(split2[1]);
12+
if (cmp != 0) return cmp;
13+
return split1[0].compareTo(split2[0]);
14+
}
15+
return isDigit1 ? (isDigit2 ? 0 : 1) : -1;
16+
});
17+
return logs;
18+
}
19+
}

python/937_Reorder_Log_Files.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
# def reorderLogFiles(self, logs):
3+
# """
4+
# :type logs: List[str]
5+
# :rtype: List[str]
6+
# """
7+
# def f(log):
8+
# id_, rest = log.split(" ", 1)
9+
# return (0, rest, id_) if rest[0].isalpha() else (1,)
10+
11+
# # Python sort is stable, so digit with keep their order
12+
# return sorted(logs, key = f)
13+
14+
def reorderLogFiles(self, logs):
15+
letter_logs = []
16+
digit_logs = []
17+
for log in logs:
18+
if log.split(' ')[1].isnumeric():
19+
digit_logs.append(log)
20+
else:
21+
letter_logs.append(log)
22+
return sorted(letter_logs, key=lambda x: x.split(' ')[1:] + x.split(' ')[0]) + digit_logs
23+

0 commit comments

Comments
 (0)