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 a407222

Browse files
feat: add solutions to lc problem: No.0388
No.0388.Longest Absolute File Path
1 parent bbb5113 commit a407222

File tree

6 files changed

+508
-1
lines changed

6 files changed

+508
-1
lines changed

‎solution/0300-0399/0388.Longest Absolute File Path/README.md

Lines changed: 174 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,195 @@ dir
7979

8080
<!-- 这里可写通用的实现逻辑 -->
8181

82+
遍历文件系统的时候需要在各个目录间切换,在实际的 Linux 中,有 `pushd``popd` 命令,本题可以使用栈模拟这一过程
83+
8284
<!-- tabs:start -->
8385

8486
### **Python3**
8587

8688
<!-- 这里可写当前语言的特殊实现逻辑 -->
8789

8890
```python
89-
91+
class Solution:
92+
def lengthLongestPath(self, input: str) -> int:
93+
i, n = 0, len(input)
94+
ans = 0
95+
stk = []
96+
while i < n:
97+
ident = 0
98+
while input[i] == '\t':
99+
ident += 1
100+
i += 1
101+
102+
cur, isFile = 0, False
103+
while i < n and input[i] != '\n':
104+
cur += 1
105+
if input[i] == '.':
106+
isFile = True
107+
i += 1
108+
i += 1
109+
110+
# popd
111+
while len(stk) > 0 and len(stk) > ident:
112+
stk.pop()
113+
114+
if len(stk) > 0:
115+
cur += stk[-1] + 1
116+
117+
# pushd
118+
if not isFile:
119+
stk.append(cur)
120+
continue
121+
122+
ans = max(ans, cur)
123+
124+
return ans
90125
```
91126

92127
### **Java**
93128

94129
<!-- 这里可写当前语言的特殊实现逻辑 -->
95130

96131
```java
132+
class Solution {
133+
public int lengthLongestPath(String input) {
134+
int i = 0;
135+
int n = input.length();
136+
int ans = 0;
137+
Deque<Integer> stack = new ArrayDeque<>();
138+
while (i < n) {
139+
int ident = 0;
140+
for (; input.charAt(i) == '\t'; i++) {
141+
ident++;
142+
}
143+
144+
int cur = 0;
145+
boolean isFile = false;
146+
for (; i < n && input.charAt(i) != '\n'; i++) {
147+
cur++;
148+
if (input.charAt(i) == '.') {
149+
isFile = true;
150+
}
151+
}
152+
i++;
153+
154+
// popd
155+
while (!stack.isEmpty() && stack.size() > ident) {
156+
stack.pop();
157+
}
158+
159+
if (stack.size() > 0) {
160+
cur += stack.peek() + 1;
161+
}
162+
163+
// pushd
164+
if (!isFile) {
165+
stack.push(cur);
166+
continue;
167+
}
168+
169+
ans = Math.max(ans, cur);
170+
}
171+
return ans;
172+
}
173+
}
174+
```
175+
176+
### **Go**
177+
178+
```go
179+
func lengthLongestPath(input string) int {
180+
i, n := 0, len(input)
181+
ans := 0
182+
var stk []int
183+
for i < n {
184+
ident := 0
185+
for ; input[i] == '\t'; i++ {
186+
ident++
187+
}
188+
189+
cur, isFile := 0, false
190+
for ; i < n && input[i] != '\n'; i++ {
191+
cur++
192+
if input[i] == '.' {
193+
isFile = true
194+
}
195+
}
196+
i++
197+
198+
// popd
199+
for len(stk) > 0 && len(stk) > ident {
200+
stk = stk[:len(stk)-1]
201+
}
202+
203+
if len(stk) > 0 {
204+
cur += stk[len(stk)-1] + 1
205+
}
206+
207+
// pushd
208+
if !isFile {
209+
stk = append(stk, cur)
210+
continue
211+
}
212+
213+
ans = max(ans, cur)
214+
}
215+
return ans
216+
}
217+
218+
func max(x, y int) int {
219+
if x > y {
220+
return x
221+
}
222+
return y
223+
}
224+
```
97225

226+
### **C++**
227+
228+
```cpp
229+
class Solution {
230+
public:
231+
int lengthLongestPath(string input) {
232+
int i = 0, n = input.size();
233+
int ans = 0;
234+
stack<int> stk;
235+
while (i < n) {
236+
int ident = 0;
237+
for (; input[i] == '\t'; ++i) {
238+
++ident;
239+
}
240+
241+
int cur = 0;
242+
bool isFile = false;
243+
for (; i < n && input[i] != '\n'; ++i) {
244+
++cur;
245+
if (input[i] == '.') {
246+
isFile = true;
247+
}
248+
}
249+
++i;
250+
251+
// popd
252+
while (!stk.empty() && stk.size() > ident) {
253+
stk.pop();
254+
}
255+
256+
if (stk.size() > 0) {
257+
cur += stk.top() + 1;
258+
}
259+
260+
// pushd
261+
if (!isFile) {
262+
stk.push(cur);
263+
continue;
264+
}
265+
266+
ans = max(ans, cur);
267+
}
268+
return ans;
269+
}
270+
};
98271
```
99272

100273
### **...**

‎solution/0300-0399/0388.Longest Absolute File Path/README_EN.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,184 @@ We return 32 since it is the longest absolute path to a file.
7171
### **Python3**
7272

7373
```python
74+
class Solution:
75+
def lengthLongestPath(self, input: str) -> int:
76+
i, n = 0, len(input)
77+
ans = 0
78+
stk = []
79+
while i < n:
80+
ident = 0
81+
while input[i] == '\t':
82+
ident += 1
83+
i += 1
7484

85+
cur, isFile = 0, False
86+
while i < n and input[i] != '\n':
87+
cur += 1
88+
if input[i] == '.':
89+
isFile = True
90+
i += 1
91+
i += 1
92+
93+
# popd
94+
while len(stk) > 0 and len(stk) > ident:
95+
stk.pop()
96+
97+
if len(stk) > 0:
98+
cur += stk[-1] + 1
99+
100+
# pushd
101+
if not isFile:
102+
stk.append(cur)
103+
continue
104+
105+
ans = max(ans, cur)
106+
107+
return ans
75108
```
76109

77110
### **Java**
78111

79112
```java
113+
class Solution {
114+
public int lengthLongestPath(String input) {
115+
int i = 0;
116+
int n = input.length();
117+
int ans = 0;
118+
Deque<Integer> stack = new ArrayDeque<>();
119+
while (i < n) {
120+
int ident = 0;
121+
for (; input.charAt(i) == '\t'; i++) {
122+
ident++;
123+
}
124+
125+
int cur = 0;
126+
boolean isFile = false;
127+
for (; i < n && input.charAt(i) != '\n'; i++) {
128+
cur++;
129+
if (input.charAt(i) == '.') {
130+
isFile = true;
131+
}
132+
}
133+
i++;
134+
135+
// popd
136+
while (!stack.isEmpty() && stack.size() > ident) {
137+
stack.pop();
138+
}
139+
140+
if (stack.size() > 0) {
141+
cur += stack.peek() + 1;
142+
}
143+
144+
// pushd
145+
if (!isFile) {
146+
stack.push(cur);
147+
continue;
148+
}
149+
150+
ans = Math.max(ans, cur);
151+
}
152+
return ans;
153+
}
154+
}
155+
```
156+
157+
### **Go**
158+
159+
```go
160+
func lengthLongestPath(input string) int {
161+
i, n := 0, len(input)
162+
ans := 0
163+
var stk []int
164+
for i < n {
165+
ident := 0
166+
for ; input[i] == '\t'; i++ {
167+
ident++
168+
}
169+
170+
cur, isFile := 0, false
171+
for ; i < n && input[i] != '\n'; i++ {
172+
cur++
173+
if input[i] == '.' {
174+
isFile = true
175+
}
176+
}
177+
i++
178+
179+
// popd
180+
for len(stk) > 0 && len(stk) > ident {
181+
stk = stk[:len(stk)-1]
182+
}
183+
184+
if len(stk) > 0 {
185+
cur += stk[len(stk)-1] + 1
186+
}
187+
188+
// pushd
189+
if !isFile {
190+
stk = append(stk, cur)
191+
continue
192+
}
193+
194+
ans = max(ans, cur)
195+
}
196+
return ans
197+
}
198+
199+
func max(x, y int) int {
200+
if x > y {
201+
return x
202+
}
203+
return y
204+
}
205+
```
206+
207+
### **C++**
208+
209+
```cpp
210+
class Solution {
211+
public:
212+
int lengthLongestPath(string input) {
213+
int i = 0, n = input.size();
214+
int ans = 0;
215+
stack<int> stk;
216+
while (i < n) {
217+
int ident = 0;
218+
for (; input[i] == '\t'; ++i) {
219+
++ident;
220+
}
221+
222+
int cur = 0;
223+
bool isFile = false;
224+
for (; i < n && input[i] != '\n'; ++i) {
225+
++cur;
226+
if (input[i] == '.') {
227+
isFile = true;
228+
}
229+
}
230+
++i;
231+
232+
// popd
233+
while (!stk.empty() && stk.size() > ident) {
234+
stk.pop();
235+
}
236+
237+
if (stk.size() > 0) {
238+
cur += stk.top() + 1;
239+
}
240+
241+
// pushd
242+
if (!isFile) {
243+
stk.push(cur);
244+
continue;
245+
}
80246

247+
ans = max(ans, cur);
248+
}
249+
return ans;
250+
}
251+
};
81252
```
82253

83254
### **...**

0 commit comments

Comments
(0)

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