Skip to content

Commit 3ba2b2b

Browse files
aQuaaQua
aQua
authored and
aQua
committed
71 added
1 parent bc15b66 commit 3ba2b2b

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [71. Simplify Path](https://leetcode.com/problems/simplify-path/)
2+
3+
## 题目
4+
Given an absolute path for a file (Unix-style), simplify it.
5+
6+
```
7+
For example,
8+
path = "/home/", => "/home"
9+
path = "/a/./b/../../c/", => "/c"
10+
```
11+
12+
click to show corner cases.
13+
14+
Corner Cases:
15+
```
16+
Did you consider the case where path = "/../"?
17+
In this case, you should return "/".
18+
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
19+
In this case, you should ignore redundant slashes and return "/home/foo".
20+
```
21+
## 解题思路
22+
23+
见程序注释
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package Problem0071
2+
3+
func simplifyPath(path string) string {
4+
5+
return "/home"
6+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package Problem0071
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
path string
18+
}
19+
20+
// ans 是答案
21+
type ans struct {
22+
one string
23+
}
24+
25+
func Test_Problem0071(t *testing.T) {
26+
ast := assert.New(t)
27+
28+
qs := []question{
29+
30+
question{
31+
para{
32+
"/home/",
33+
},
34+
ans{
35+
"/home",
36+
},
37+
},
38+
39+
question{
40+
para{
41+
"/a/./b/../../c/",
42+
},
43+
ans{
44+
"/c",
45+
},
46+
},
47+
48+
question{
49+
para{
50+
"/../",
51+
},
52+
ans{
53+
"/",
54+
},
55+
},
56+
57+
question{
58+
para{
59+
"/home//foo/",
60+
},
61+
ans{
62+
"/home/foo",
63+
},
64+
},
65+
66+
// 如需多个测试,可以复制上方元素。
67+
}
68+
69+
for _, q := range qs {
70+
a, p := q.ans, q.para
71+
fmt.Printf("~~%v~~\n", p)
72+
73+
ast.Equal(a.one, simplifyPath(p.path), "输入:%v", p)
74+
}
75+
}

0 commit comments

Comments
 (0)