Skip to content

Commit 692f4ad

Browse files
author
SimmyZhong
committed
feat: 71简化路径
1 parent a2b2155 commit 692f4ad

File tree

5 files changed

+153
-8
lines changed

5 files changed

+153
-8
lines changed

70_climbing-stairs.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
https://leetcode-cn.com/problems/climbing-stairs/
3+
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
4+
5+
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
6+
7+
注意:给定 n 是一个正整数。
8+
9+
示例 1:
10+
11+
输入: 2
12+
输出: 2
13+
解释: 有两种方法可以爬到楼顶。
14+
1. 1 阶 + 1 阶
15+
2. 2 阶
16+
示例 2:
17+
18+
输入: 3
19+
输出: 3
20+
解释: 有三种方法可以爬到楼顶。
21+
1. 1 阶 + 1 阶 + 1 阶
22+
2. 1 阶 + 2 阶
23+
3. 2 阶 + 1 阶
24+
*/
25+
26+
package main
27+
28+
import "fmt"
29+
30+
func main() {
31+
fmt.Println(climbStairs(4))
32+
}
33+
34+
func climbStairs(n int) int {
35+
choice := []int{1, 2}
36+
for i:=2; i < n; i++ {
37+
choice = append(choice, choice[i-1] + choice[i-2])
38+
}
39+
return choice[n-1]
40+
}

71_simplify-path.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
https://leetcode-cn.com/problems/simplify-path/
3+
以 Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。
4+
5+
在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。更多信息请参阅:Linux / Unix中的绝对路径 vs 相对路径
6+
7+
请注意,返回的规范路径必须始终以斜杠 / 开头,并且两个目录名之间必须只有一个斜杠 /。最后一个目录名(如果存在)不能以 / 结尾。此外,规范路径必须是表示绝对路径的最短字符串。
8+
9+
示例 1:
10+
11+
输入:"/home/"
12+
输出:"/home"
13+
解释:注意,最后一个目录名后面没有斜杠。
14+
示例 2:
15+
16+
输入:"/../"
17+
输出:"/"
18+
解释:从根目录向上一级是不可行的,因为根是你可以到达的最高级。
19+
示例 3:
20+
21+
输入:"/home//foo/"
22+
输出:"/home/foo"
23+
解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。
24+
示例 4:
25+
26+
输入:"/a/./b/../../c/"
27+
输出:"/c"
28+
示例 5:
29+
30+
输入:"/a/../../b/../c//.//"
31+
输出:"/c"
32+
示例 6:
33+
34+
输入:"/a//b////c/d//././/.."
35+
输出:"/a/b/c"
36+
"""
37+
38+
class Solution:
39+
def simplifyPath(self, path):
40+
paths = path.split("/")
41+
stack = []
42+
for each in paths:
43+
if each in (".", ""):
44+
continue
45+
if each == "..":
46+
if stack:
47+
stack.pop()
48+
else:
49+
stack.append(each)
50+
return "/" + "/".join(stack)
51+
52+
53+
def main():
54+
print(Solution().simplifyPath("/home//foo//"))
55+
56+
57+
if __name__ == '__main__':
58+
main()

golang/98_validate-binary-search-tree.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,8 @@ func isValid(node *TreeNode, minVal, maxVal int) bool {
7777

7878
}
7979

80+
func main() {
81+
fmt.Println(^uint(0)*2)
82+
}
8083

8184

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272

7373
&emsp;&emsp;058 [最后一个单词的长度](https://github.com/SimmyZhong/leetCode/blob/master/golang/58_length-of-last-word.go)
7474

75+
&emsp;&emsp;071 [简化路径](https://github.com/SimmyZhong/leetCode/blob/master/71_simplify-path.py)
76+
7577
&emsp;&emsp;387 [字符串中第一个唯一字符](https://github.com/SimmyZhong/leetCode/blob/master/387_first-unique-character-in-a-string.py)
7678

7779
&emsp;&emsp;524 [通过删除字母匹配到字典里最长单词](https://github.com/SimmyZhong/leetCode/blob/master/524_longest-word-in-dictionary-through-deleting.py)

test.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
1-
class Node(object):
21

3-
def __init__(self, val):
4-
self.val = val
2+
import sys, string
3+
# class Solution:
4+
# def coinChange(self, nums, target):
5+
# resp =[0] * (target+1)
6+
# for i in range(1, target+1):
7+
# cur = sys.maxsize
8+
# for each in nums:
9+
# if i - each >= 0:
10+
# cur = min(resp[i-each]+1, cur)
11+
# resp[i] = cur
12+
# return resp[target] if resp[target] != sys.maxsize else -1
513

6-
def __getitem__(self, k):
7-
return getattr(self, k)
14+
# print(Solution().coinChange([2],3))
815

9-
a = Node('ac')
10-
print(a["val"])
11-
print(a.val)
16+
17+
def test(a, b):
18+
choice = dict(zip('0123456789'+string.ascii_lowercase, range(36)))
19+
choice2 = dict(zip(range(36), '0123456789'+string.ascii_lowercase))
20+
len_a, len_b = len(a), len(b)
21+
lens = len_a+1 if len_a > len_b else len_b+1
22+
resp = [0] * lens
23+
flag = False
24+
for i in range(lens):
25+
if len_a - i > 0:
26+
resp[i] += choice[a[len_a-i-1]]
27+
if len_b - i > 0:
28+
resp[i] += choice[b[len_b-i-1]]
29+
if flag:
30+
resp[i] += 1
31+
if resp[i] > 35:
32+
resp[i] -= 35
33+
flag = True
34+
else:
35+
flag = False
36+
resp[i] = choice2[resp[i]]
37+
return ''.join(resp)
38+
39+
# print(test('abc', 'adb'))
40+
41+
def test(s):
42+
if not s:
43+
return 0
44+
i, resp, mapping = 0, 0, {}
45+
for j in range(len(s)):
46+
word = s[j]
47+
if word in mapping and mapping[word] >= i:
48+
resp = max(j-i, resp)
49+
i = mapping[word] + 1
50+
mapping[word] = j
51+
return max(resp, j-i+1)
52+
53+
print(test('abca'))

0 commit comments

Comments
 (0)