Skip to content

Commit 1755697

Browse files
committed
✨feat: Add 937
1 parent dd75bab commit 1755697

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

Index/排序.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 |
1515
| [703. 数据流中的第 K 大元素](https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/solution/jian-da-ti-de-duo-chong-jie-fa-mou-pao-p-d1qi/) | 简单 | 🤩🤩🤩 |
1616
| [825. 适龄的朋友](https://leetcode-cn.com/problems/friends-of-appropriate-ages/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/friends-of-appropriate-ages/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-maa8/) | 中等 | 🤩🤩🤩🤩 |
17+
| [937. 重新排列日志文件](https://leetcode-cn.com/problems/reorder-data-in-log-files/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reorder-data-in-log-files/solution/by-ac_oier-ap28/) | 简单 | 🤩🤩🤩🤩🤩 |
1718
| [954. 二倍数对数组](https://leetcode-cn.com/problems/array-of-doubled-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/array-of-doubled-pairs/solution/by-ac_oier-d1z7/) | 中等 | 🤩🤩🤩 |
1819
| [969. 煎饼排序](https://leetcode-cn.com/problems/pancake-sorting/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/pancake-sorting/solution/gong-shui-san-xie-mou-pao-pai-xu-yun-yon-c0mn/) | 中等 | 🤩🤩🤩🤩🤩 |
1920
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[937. 重新排列日志文件](https://leetcode-cn.com/problems/reorder-data-in-log-files/solution/by-ac_oier-ap28/)** ,难度为 **简单**
4+
5+
Tag : 「排序」
6+
7+
8+
9+
给你一个日志数组 logs。每条日志都是以空格分隔的字串,其第一个字为字母与数字混合的 标识符 。
10+
11+
有两种不同类型的日志:
12+
13+
* 字母日志:除标识符之外,所有字均由小写字母组成
14+
* 数字日志:除标识符之外,所有字均由数字组成
15+
16+
请按下述规则将日志重新排序:
17+
18+
* 所有 字母日志 都排在 数字日志 之前。
19+
* 字母日志 在内容不同时,忽略标识符后,按内容字母顺序排序;在内容相同时,按标识符排序。
20+
* 数字日志 应该保留原来的相对顺序。
21+
22+
返回日志的最终顺序。
23+
24+
示例 1:
25+
```
26+
输入:logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
27+
28+
输出:["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
29+
30+
解释:
31+
字母日志的内容都不同,所以顺序为 "art can", "art zero", "own kit dig" 。
32+
数字日志保留原来的相对顺序 "dig1 8 1 5 1", "dig2 3 6" 。
33+
```
34+
示例 2:
35+
```
36+
输入:logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
37+
38+
输出:["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
39+
```
40+
41+
提示:
42+
* $1 <= logs.length <= 100$
43+
* $3 <= logs[i].length <= 100$
44+
* $logs[i]$ 中,字与字之间都用 单个 空格分隔
45+
* 题目数据保证 $logs[i]$ 都有一个标识符,并且在标识符之后至少存在一个字
46+
47+
---
48+
49+
### 自定义类 + 排序
50+
51+
根据排序规则,我们需要对每个 $str[i]$ 进行裁剪处理,从而得知每个 $str[i]$ 是属于「字母日志」还是「数字日志」,以及得到 `sign` 部分和 `content` 部分。
52+
53+
在排序过程中,每个 $str[i]$ 会被访问多次,为了让每个 $str[i]$ 只进行一次这样的预处理工作,我们可以自定义类,将这部分工作放到类的实例化去做。
54+
55+
最后是简单将 $str[i]$ 转存成 `Log` 实例,自定义排序,用排序结果构造答案的基本逻辑。
56+
57+
代码:
58+
```Java
59+
class Solution {
60+
class Log {
61+
int type, idx;
62+
String ori, sign, content;
63+
Log(String s, int _idx) {
64+
idx = _idx;
65+
int n = s.length(), i = 0;
66+
while (i < n && s.charAt(i) != ' ') i++;
67+
sign = s.substring(0, i);
68+
content = s.substring(i + 1);
69+
ori = s;
70+
type = Character.isDigit(content.charAt(0)) ? 1 : 0;
71+
}
72+
}
73+
public String[] reorderLogFiles(String[] logs) {
74+
int n = logs.length;
75+
List<Log> list = new ArrayList<>();
76+
for (int i = 0; i < n; i++) list.add(new Log(logs[i], i));
77+
Collections.sort(list, (a, b)->{
78+
if (a.type != b.type) return a.type - b.type;
79+
if (a.type == 1) return a.idx - b.idx;
80+
return !a.content.equals(b.content) ? a.content.compareTo(b.content) : a.sign.compareTo(b.sign);
81+
});
82+
String[] ans = new String[n];
83+
for (int i = 0; i < n; i++) ans[i] = list.get(i).ori;
84+
return ans;
85+
}
86+
}
87+
```
88+
* 时间复杂度:将所有的 $str[i]$ 转存成 `Log` 实例,复杂度为 $O(n)$;排序复杂度为 $O(n\log{n})$;构造答案复杂度为 $O(n)$。整体复杂度为 $O(n\log{n})$
89+
* 空间复杂度:$O(n)$
90+
91+
---
92+
93+
### 最后
94+
95+
这是我们「刷穿 LeetCode」系列文章的第 `No.937` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
96+
97+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
98+
99+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
100+
101+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
102+

0 commit comments

Comments
 (0)