Skip to content

Commit 4056bb4

Browse files
committed
feat: add solutions to lc problem: No.0071
No.0071.Simplify Path
1 parent f407eb3 commit 4056bb4

File tree

5 files changed

+150
-22
lines changed

5 files changed

+150
-22
lines changed

solution/0000-0099/0071.Simplify Path/README.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,77 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
栈实现。
71+
7072
<!-- tabs:start -->
7173

7274
### **Python3**
7375

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

7678
```python
77-
79+
class Solution:
80+
def simplifyPath(self, path: str) -> str:
81+
stk = []
82+
for s in path.split('/'):
83+
if not s or s == '.':
84+
continue
85+
if s == '..':
86+
if stk:
87+
stk.pop()
88+
else:
89+
stk.append(s)
90+
return '/' + '/'.join(stk)
7891
```
7992

8093
### **Java**
8194

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

8497
```java
98+
class Solution {
99+
public String simplifyPath(String path) {
100+
Deque<String> stk = new ArrayDeque<>();
101+
for (String s : path.split("/")) {
102+
if ("".equals(s) || ".".equals(s)) {
103+
continue;
104+
}
105+
if ("..".equals(s)) {
106+
stk.pollLast();
107+
} else {
108+
stk.offerLast(s);
109+
}
110+
}
111+
return "/" + String.join("/", stk);
112+
}
113+
}
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func simplifyPath(path string) string {
120+
var stk []string
121+
for _, s := range strings.Split(path, "/") {
122+
if s == "" || s == "." {
123+
continue
124+
}
125+
if s == ".." {
126+
if len(stk) > 0 {
127+
stk = stk[0 : len(stk)-1]
128+
}
129+
} else {
130+
stk = append(stk, s)
131+
}
132+
}
133+
return "/" + strings.Join(stk, "/")
134+
}
135+
```
85136

137+
```go
138+
func simplifyPath(path string) string {
139+
return filepath.Clean(path)
140+
}
86141
```
87142

88143
### **...**

solution/0000-0099/0071.Simplify Path/README_EN.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,73 @@
6262

6363
## Solutions
6464

65+
Using stack.
66+
6567
<!-- tabs:start -->
6668

6769
### **Python3**
6870

6971
```python
70-
72+
class Solution:
73+
def simplifyPath(self, path: str) -> str:
74+
stk = []
75+
for s in path.split('/'):
76+
if not s or s == '.':
77+
continue
78+
if s == '..':
79+
if stk:
80+
stk.pop()
81+
else:
82+
stk.append(s)
83+
return '/' + '/'.join(stk)
7184
```
7285

7386
### **Java**
7487

7588
```java
89+
class Solution {
90+
public String simplifyPath(String path) {
91+
Deque<String> stk = new ArrayDeque<>();
92+
for (String s : path.split("/")) {
93+
if ("".equals(s) || ".".equals(s)) {
94+
continue;
95+
}
96+
if ("..".equals(s)) {
97+
stk.pollLast();
98+
} else {
99+
stk.offerLast(s);
100+
}
101+
}
102+
return "/" + String.join("/", stk);
103+
}
104+
}
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func simplifyPath(path string) string {
111+
var stk []string
112+
for _, s := range strings.Split(path, "/") {
113+
if s == "" || s == "." {
114+
continue
115+
}
116+
if s == ".." {
117+
if len(stk) > 0 {
118+
stk = stk[0 : len(stk)-1]
119+
}
120+
} else {
121+
stk = append(stk, s)
122+
}
123+
}
124+
return "/" + strings.Join(stk, "/")
125+
}
126+
```
76127

128+
```go
129+
func simplifyPath(path string) string {
130+
return filepath.Clean(path)
131+
}
77132
```
78133

79134
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func simplifyPath(path string) string {
2+
var stk []string
3+
for _, s := range strings.Split(path, "/") {
4+
if s == "" || s == "." {
5+
continue
6+
}
7+
if s == ".." {
8+
if len(stk) > 0 {
9+
stk = stk[0 : len(stk)-1]
10+
}
11+
} else {
12+
stk = append(stk, s)
13+
}
14+
}
15+
return "/" + strings.Join(stk, "/")
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
class Solution {
22
public String simplifyPath(String path) {
3-
List<String> dirs = new ArrayList<>();
4-
int dirStart = 0, len = path.length();
5-
while (dirStart < len) {
6-
while (dirStart < len && path.charAt(dirStart) == '/') dirStart++;
7-
int dirEnd = dirStart;
8-
while (dirEnd < len && path.charAt(dirEnd) != '/') dirEnd++;
9-
String dir = path.substring(dirStart, dirEnd);
10-
if (!".".equals(dir)) {
11-
if ("..".equals(dir)) {
12-
if (! dirs.isEmpty()) dirs.remove(dirs.size() - 1);
13-
} else if (dir.length() > 0) {
14-
dirs.add(dir);
15-
}
3+
Deque<String> stk = new ArrayDeque<>();
4+
for (String s : path.split("/")) {
5+
if ("".equals(s) || ".".equals(s)) {
6+
continue;
7+
}
8+
if ("..".equals(s)) {
9+
stk.pollLast();
10+
} else {
11+
stk.offerLast(s);
1612
}
17-
dirStart = ++dirEnd;
18-
}
19-
StringBuilder sb = new StringBuilder("/");
20-
for (int i = 0; i < dirs.size(); i++) {
21-
if (i == dirs.size() - 1) sb.append(dirs.get(i));
22-
else sb.append(dirs.get(i)).append("/");
2313
}
24-
return sb.toString();
14+
return "/" + String.join("/", stk);
2515
}
2616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def simplifyPath(self, path: str) -> str:
3+
stk = []
4+
for s in path.split('/'):
5+
if not s or s == '.':
6+
continue
7+
if s == '..':
8+
if stk:
9+
stk.pop()
10+
else:
11+
stk.append(s)
12+
return '/' + '/'.join(stk)

0 commit comments

Comments
 (0)