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

Commit d89a8ac

Browse files
✨feat: Add 953
1 parent ca8122e commit d89a8ac

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

‎Index/排序.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| [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/) | 简单 | 🤩🤩🤩 |
1717
| [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/) | 中等 | 🤩🤩🤩🤩 |
1818
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
19+
| [953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/) | 简单 | 🤩🤩🤩🤩 |
1920
| [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/) | 中等 | 🤩🤩🤩 |
2021
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
2122
| [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/) | 困难 | 🤩🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
106106
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩 |
107107
| [944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/) | 简单 | 🤩🤩🤩 |
108+
| [953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/) | 简单 | 🤩🤩🤩🤩 |
108109
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
109110
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
110111
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/)** ,难度为 **简单**
4+
5+
Tag : 「排序」、「模拟」
6+
7+
8+
9+
某种外星语也使用英文小写字母,但可能顺序 `order` 不同。字母表的顺序(`order`)是一些小写字母的排列。
10+
11+
给定一组用外星语书写的单词 `words`,以及其字母表的顺序 `order`,只有当给定的单词在这种外星语中按字典序排列时,返回 `true`;否则,返回 `false`
12+
13+
示例 1:
14+
```
15+
输入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
16+
17+
输出:true
18+
19+
解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。
20+
```
21+
示例 2:
22+
```
23+
输入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
24+
25+
输出:false
26+
27+
解释:在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。
28+
```
29+
示例 3:
30+
```
31+
输入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
32+
33+
输出:false
34+
35+
解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。
36+
```
37+
38+
提示:
39+
* 1ドル <= words.length <= 100$
40+
* 1ドル <= words[i].length <= 20$
41+
* $order.length == 26$
42+
*`words[i]``order` 中的所有字符都是英文小写字母。
43+
44+
---
45+
46+
### 自定义排序
47+
48+
为了快速判断某两个字符在字典序的前后关系,先使用一个大小与字符集相等的数组对 `order` 进行转存。
49+
50+
然后对 `words` 进行拷贝复制得到 `clone`,并执行自定义排序,最后根据排序前后顺序是否相等来返回答案。
51+
52+
代码:
53+
```Java
54+
class Solution {
55+
public boolean isAlienSorted(String[] words, String order) {
56+
int[] ord = new int[26];
57+
for (int i = 0; i < 26; i++) ord[order.charAt(i) - 'a'] = i;
58+
String[] clone = words.clone();
59+
Arrays.sort(clone, (a, b)->{
60+
int n = a.length(), m = b.length();
61+
int i = 0, j = 0;
62+
while (i < n && j < m) {
63+
int c1 = a.charAt(i) - 'a', c2 = b.charAt(j) - 'a';
64+
if (c1 != c2) return ord[c1] - ord[c2];
65+
i++; j++;
66+
}
67+
if (i < n) return 1;
68+
if (j < m) return -1;
69+
return 0;
70+
});
71+
int n = words.length;
72+
for (int i = 0; i < n; i++) {
73+
if (!clone[i].equals(words[i])) return false;
74+
}
75+
return true;
76+
}
77+
}
78+
```
79+
* 时间复杂度:$O(n\log{n})$
80+
* 空间复杂度:$O(\log{n})$
81+
82+
---
83+
84+
### 模拟
85+
86+
更近一步,我们无须对整个数组进行排序,只需要将「自定义排序」中的规则抽出来,判断相邻两个字符串是满足字典序排序即可。
87+
88+
代码:
89+
```Java
90+
class Solution {
91+
int[] ord = new int[26];
92+
int check(String a, String b) {
93+
int n = a.length(), m = b.length();
94+
int i = 0, j = 0;
95+
while (i < n && j < m) {
96+
int c1 = a.charAt(i) - 'a', c2 = b.charAt(j) - 'a';
97+
if (c1 != c2) return ord[c1] - ord[c2];
98+
i++; j++;
99+
}
100+
if (i < n) return 1;
101+
if (j < m) return -1;
102+
return 0;
103+
}
104+
public boolean isAlienSorted(String[] words, String order) {
105+
for (int i = 0; i < 26; i++) ord[order.charAt(i) - 'a'] = i;
106+
int n = words.length;
107+
for (int i = 1; i < n; i++) {
108+
if (check(words[i - 1], words[i]) > 0) return false;
109+
}
110+
return true;
111+
}
112+
}
113+
```
114+
* 时间复杂度:$O(n)$
115+
* 空间复杂度:$O(C)$
116+
117+
---
118+
119+
### 最后
120+
121+
这是我们「刷穿 LeetCode」系列文章的第 `No.953` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
122+
123+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
124+
125+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
126+
127+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
128+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /