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 7bd68c4

Browse files
Merge pull request SharingSource#477 from SharingSource/ac_oier
✨feat: Add 824 & 388
2 parents f0d84ba + 1e3e56d commit 7bd68c4

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
| [268. 丢失的数字](https://leetcode-cn.com/problems/missing-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/missing-number/solution/gong-shui-san-xie-yi-ti-wu-jie-pai-xu-ji-te3s/) | 简单 | 🤩🤩🤩🤩 |
1818
| [299. 猜数字游戏](https://leetcode-cn.com/problems/bulls-and-cows/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/bulls-and-cows/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-tdhs/) | 中等 | 🤩🤩🤩🤩 |
1919
| [380. O(1) 时间插入、删除和获取随机元素](https://leetcode-cn.com/problems/insert-delete-getrandom-o1/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/insert-delete-getrandom-o1/solution/by-ac_oier-tpex/) | 中等 | 🤩🤩🤩🤩🤩 |
20+
| [388. 文件的最长绝对路径](https://leetcode-cn.com/problems/longest-absolute-file-path/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-absolute-file-path/solution/by-ac_oier-c55t/) | 中等 | 🤩🤩🤩🤩🤩 |
2021
| [318. 最大单词长度乘积](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/gong-shui-san-xie-jian-dan-wei-yun-suan-cqtxq/) | 中等 | 🤩🤩🤩🤩 |
2122
| [432. 全 O(1) 的数据结构](https://leetcode-cn.com/problems/all-oone-data-structure/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-oone-data-structure/solution/by-ac_oier-t26d/) | 困难 | 🤩🤩🤩 |
2223
| [447. 回旋镖的数量](https://leetcode-cn.com/problems/number-of-boomerangs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-boomerangs/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-evu2/) | 中等 | 🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
| [382. 链表随机节点](https://leetcode-cn.com/problems/linked-list-random-node/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/linked-list-random-node/solution/gong-shui-san-xie-xu-shui-chi-chou-yang-1lp9d/) | 中等 | 🤩🤩🤩🤩🤩 |
4242
| [383. 赎金信](https://leetcode-cn.com/problems/ransom-note/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ransom-note/solution/gong-shui-san-xie-jian-dan-ji-shu-mo-ni-udpzn/) | 简单 | 🤩🤩🤩🤩🤩 |
4343
| [385. 迷你语法分析器](https://leetcode-cn.com/problems/mini-parser/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/mini-parser/solution/by-ac_oier-zuy6/) | 中等 | 🤩🤩🤩🤩🤩 |
44+
| [388. 文件的最长绝对路径](https://leetcode-cn.com/problems/longest-absolute-file-path/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-absolute-file-path/solution/by-ac_oier-c55t/) | 中等 | 🤩🤩🤩🤩🤩 |
4445
| [393. UTF-8 编码验证](https://leetcode-cn.com/problems/utf-8-validation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/utf-8-validation/solution/by-ac_oier-zvoy/) | 中等 | 🤩🤩🤩🤩🤩 |
4546
| [400. 第 N 位数字](https://leetcode-cn.com/problems/nth-digit/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/nth-digit/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-w5wl/) | 中等 | 🤩🤩🤩🤩 |
4647
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[388. 文件的最长绝对路径](https://leetcode-cn.com/problems/longest-absolute-file-path/solution/by-ac_oier-c55t/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「哈希表」
6+
7+
8+
9+
假设有一个同时存储文件和目录的文件系统。下图展示了文件系统的一个示例:
10+
11+
![](https://assets.leetcode.com/uploads/2020/08/28/mdir.jpg)
12+
13+
这里将 `dir` 作为根目录中的唯一目录。`dir` 包含两个子目录 `subdir1``subdir2`
14+
15+
`subdir1` 包含文件 `file1.ext` 和子目录 `subsubdir1`;`subdir2` 包含子目录 `subsubdir2`,该子目录下包含文件 `file2.ext`
16+
17+
在文本格式中,如下所示(⟶表示制表符):
18+
```
19+
dir
20+
⟶ subdir1
21+
⟶ ⟶ file1.ext
22+
⟶ ⟶ subsubdir1
23+
⟶ subdir2
24+
⟶ ⟶ subsubdir2
25+
⟶ ⟶ ⟶ file2.ext
26+
```
27+
28+
如果是代码表示,上面的文件系统可以写为 `"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"``'\n'``'\t'` 分别是换行符和制表符。
29+
30+
文件系统中的每个文件和文件夹都有一个唯一的 绝对路径 ,即必须打开才能到达文件/目录所在位置的目录顺序,所有路径用 '/' 连接。上面例子中,指向 `file2.ext` 的 绝对路径 是 `"dir/subdir2/subsubdir2/file2.ext"` 。每个目录名由字母、数字和/或空格组成,每个文件名遵循 `name.extension` 的格式,其中 `name``extension` 由字母、数字和/或空格组成。
31+
32+
给定一个以上述格式表示文件系统的字符串 `input` ,返回文件系统中 指向 文件 的 最长绝对路径 的长度 。 如果系统中没有文件,返回 0ドル$。
33+
34+
示例 1:
35+
![](https://assets.leetcode.com/uploads/2020/08/28/dir1.jpg)
36+
```
37+
输入:input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
38+
39+
输出:20
40+
41+
解释:只有一个文件,绝对路径为 "dir/subdir2/file.ext" ,路径长度 20
42+
```
43+
示例 2:
44+
![](https://assets.leetcode.com/uploads/2020/08/28/dir2.jpg)
45+
```
46+
输入:input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
47+
48+
输出:32
49+
50+
解释:存在两个文件:
51+
"dir/subdir1/file1.ext" ,路径长度 21
52+
"dir/subdir2/subsubdir2/file2.ext" ,路径长度 32
53+
返回 32 ,因为这是最长的路径
54+
```
55+
示例 3:
56+
```
57+
输入:input = "a"
58+
59+
输出:0
60+
61+
解释:不存在任何文件
62+
```
63+
示例 4:
64+
```
65+
输入:input = "file1.txt\nfile2.txt\nlongfile.txt"
66+
67+
输出:12
68+
69+
解释:根目录下有 3 个文件。
70+
因为根目录中任何东西的绝对路径只是名称本身,所以答案是 "longfile.txt" ,路径长度为 12
71+
```
72+
73+
提示:
74+
* 1ドル <= input.length <= 10^4$
75+
* `input` 可能包含小写或大写的英文字母,一个换行符 `'\n'`,一个制表符 `'\t'`,一个点 `'.'`,一个空格 `' '`,和数字。
76+
77+
---
78+
79+
### 模拟 + 哈希表
80+
81+
为了方便,我们将 `input` 替换为 `s`
82+
83+
对于每一个文件或文件夹而言,我们可以通过访问到结尾(`\n`)的方式取得,记为 `cur`,然后根据 `cur` 前面有多少个 `\t` 得知其所在的层级,假设当前其所在层级为 `level`,那么它自然归属到最新一个层级为 `level - 1` 的文件夹中,因此我们可以使用哈希表记录每个层级最新的文件夹路径,通过字符串拼接的方式得到 `cur` 所在的完整路径 `path`,并在处理整个 `s` 过程中,统计长度最大的文件路径。
84+
85+
代码:
86+
```Java
87+
class Solution {
88+
public int lengthLongestPath(String s) {
89+
Map<Integer, String> map = new HashMap<>();
90+
int n = s.length();
91+
String ans = null;
92+
for (int i = 0; i < n; ) {
93+
int level = 0;
94+
while (i < n && s.charAt(i) == '\t' && ++level >= 0) i++;
95+
int j = i;
96+
boolean isDir = true;
97+
while (j < n && s.charAt(j) != '\n') {
98+
if (s.charAt(j++) == '.') isDir = false;
99+
}
100+
String cur = s.substring(i, j);
101+
String prev = map.getOrDefault(level - 1, null);
102+
String path = prev == null ? cur : prev + "/" + cur;
103+
if (isDir) map.put(level, path);
104+
else if (ans == null || path.length() > ans.length()) ans = path;
105+
i = j + 1;
106+
}
107+
return ans == null ? 0 : ans.length();
108+
}
109+
}
110+
```
111+
* 时间复杂度:$O(n)$
112+
* 空间复杂度:$O(n)$
113+
114+
---
115+
116+
### 优化
117+
118+
上述做法只是为了方便我们输出具体方案。
119+
120+
实际上,我们只关心最终的路径长度,而不关心具体路径,因此容易将解法一修改为只记录长度,而不记录路径的做法,从而避免掉字符串拼接带来的消耗,同时利用 `s` 的长度数据范围,使用数组来替代常数较大的哈希表。
121+
122+
代码:
123+
```Java
124+
class Solution {
125+
static int[] hash = new int[10010];
126+
public int lengthLongestPath(String s) {
127+
Arrays.fill(hash, -1);
128+
int n = s.length(), ans = 0;
129+
for (int i = 0; i < n; ) {
130+
int level = 0;
131+
while (i < n && s.charAt(i) == '\t' && ++level >= 0) i++;
132+
int j = i;
133+
boolean isDir = true;
134+
while (j < n && s.charAt(j) != '\n') {
135+
if (s.charAt(j++) == '.') isDir = false;
136+
}
137+
Integer cur = j - i;
138+
Integer prev = level - 1 >= 0 ? hash[level - 1] : -1;
139+
Integer path = prev + 1 + cur;
140+
if (isDir) hash[level] = path;
141+
else if (path > ans) ans = path;
142+
i = j + 1;
143+
}
144+
return ans;
145+
}
146+
}
147+
```
148+
* 时间复杂度:$O(n)$
149+
* 空间复杂度:$O(C)$
150+
151+
---
152+
153+
### 最后
154+
155+
这是我们「刷穿 LeetCode」系列文章的第 `No.388` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
156+
157+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
158+
159+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
160+
161+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
162+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[824. 山羊拉丁文]()** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个由若干单词组成的句子 `sentence`,单词间由空格分隔。每个单词仅由大写和小写英文字母组成。
10+
11+
请你将句子转换为 "山羊拉丁文(`Goat Latin`)"(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。山羊拉丁文的规则如下:
12+
13+
* 如果单词以元音开头(`'a'`, `'e'`, `'i'`, `'o'`, `'u'`),在单词后添加`"ma"`
14+
* 例如,单词 `"apple"` 变为 `"applema"` 。
15+
* 如果单词以辅音字母开头(即,非元音字母),移除第一个字符并将它放到末尾,之后再添加`"ma"`
16+
* 例如,单词 `"goat"` 变为 `"oatgma"` 。
17+
* 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母`'a'`,索引从 1ドル$ 开始。
18+
* 例如,在第一个单词后添加 `"a"`,在第二个单词后添加 `"aa"`,以此类推。
19+
20+
返回将 `sentence` 转换为山羊拉丁文后的句子。
21+
22+
示例 1:
23+
```
24+
输入:sentence = "I speak Goat Latin"
25+
26+
输出:"Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
27+
```
28+
示例 2:
29+
```
30+
输入:sentence = "The quick brown fox jumped over the lazy dog"
31+
32+
输出:"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
33+
```
34+
35+
提示:
36+
* 1ドル <= sentence.length <= 150$
37+
* `sentence` 由英文字母和空格组成
38+
* `sentence` 不含前导或尾随空格
39+
* `sentence` 中的所有单词由单个空格分隔
40+
41+
---
42+
43+
### 模拟
44+
45+
根据题意进行模拟即可。
46+
47+
代码:
48+
```Java
49+
class Solution {
50+
public String toGoatLatin(String s) {
51+
int n = s.length();
52+
String last = "a";
53+
StringBuilder sb = new StringBuilder();
54+
for (int i = 0; i < n; ) {
55+
int j = i;
56+
while (j < n && s.charAt(j) != ' ') j++;
57+
if ("aeiouAEIOU".indexOf(s.charAt(i)) >= 0) {
58+
sb.append(s.substring(i, j)).append("ma");
59+
} else {
60+
sb.append(s.substring(i + 1, j)).append(s.charAt(i)).append("ma");
61+
}
62+
sb.append(last);
63+
last += "a";
64+
i = j + 1;
65+
if (i < n) sb.append(" ");
66+
}
67+
return sb.toString();
68+
}
69+
}
70+
```
71+
* 时间复杂度:$O(n)$
72+
* 空间复杂度:$O(n)$
73+
74+
---
75+
76+
### 最后
77+
78+
这是我们「刷穿 LeetCode」系列文章的第 `No.824` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
79+
80+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
81+
82+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
83+
84+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
85+

0 commit comments

Comments
(0)

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