File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77| [ Python爬虫] ( ./spiders/main.md ) | python爬虫基础库requests,beautifulsoup,xpath,selenium,代理池,部分网站爬虫等......|
88| [ TCP/IP与HTTP协议] ( ./network_protocol/main.md ) | 网络基础知识,tcp/ip协议与http协议等......|
99| [ LeetCode-数组] ( ./leetcode_array/main.md ) | leetcode数组类算法刷题。|
10+ | [ LeetCode-栈] ( ./leetcode_stack/main.md ) | leetcode栈类算法刷题。|
1011| [ Django学习] ( ./django_note/main.md ) | Django学习笔记。|
1112| [ 其他] ( ./others/main.md ) | redis,kafka,ZeroMQ等|
Original file line number Diff line number Diff line change 1+ ## LeetCode栈
2+ 1 . [ 有效的括号] ( 有效的括号.md )
3+
4+ 2 . [ 最小栈] ( 最小栈.md )
Original file line number Diff line number Diff line change 1+ ### 最小栈
2+
3+ 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
4+
5+ * push(x) —— 将元素 x 推入栈中。
6+ * pop() —— 删除栈顶的元素。
7+ * top() —— 获取栈顶元素。
8+ * getMin() —— 检索栈中的最小元素。
9+ * pop、top 和 getMin 操作总是在 非空栈 上调用。
10+
11+ ``` python
12+ class MinStack :
13+
14+ def __init__ (self ):
15+ """
16+ initialize your data structure here.
17+ """
18+ self .stack = list ()
19+ self .min = None
20+
21+ def push (self , x : int ) -> None :
22+ if self .min is None :
23+ self .min = x
24+ else :
25+ self .min = min (self .min, x)
26+ self .stack.append(x)
27+
28+ def pop (self ) -> None :
29+ self .stack.pop()
30+ if self .stack:
31+ self .min = min (self .stack)
32+ else :
33+ self .min = None
34+
35+ def top (self ) -> int :
36+ return self .stack[- 1 ]
37+
38+ def getMin (self ) -> int :
39+ return self .min
40+ ```
Original file line number Diff line number Diff line change 1+ ### 有效的括号
2+ 给定一个只包括 '(',')','{','}','[ ','] ' 的字符串,判断字符串是否有效。
3+
4+ 有效字符串需满足:
5+
6+ 左括号必须用相同类型的右括号闭合。
7+ 左括号必须以正确的顺序闭合。
8+ 注意空字符串可被认为是有效字符串。
9+
10+ ``` python
11+ 输入: " ()"
12+ 输出: true
13+
14+ 输入: " ()[]{} "
15+ 输出: true
16+
17+ 输入: " ([)]"
18+ 输出: false
19+ ```
20+
21+ #### 方法一:
22+ 利用字符串替换的方法,如果该字符串是有效字符串,最终总能被替换为空字符串。
23+
24+ ``` python
25+ def isValid (s ):
26+ while ' []' in s or ' ()' in s or ' {} ' in s:
27+ s = s.replace(' []' , ' ' ).replace(' {} ' , ' ' ).replace(' ()' , ' ' )
28+ if s == ' ' :
29+ return True
30+ return False
31+ ```
32+
33+ #### 方法二:
34+ * 每次遇到左括号,就入栈
35+ * 遇到右括号,如果是有效字符串,此时栈的最后一个元素肯定与该右括号匹配。
36+ * 把已经匹配的字符,从栈中弹出
37+
38+ ``` python
39+ def isValid (s ):
40+ if len (s) % 2 == 1 :
41+ return False
42+
43+ pairs = {
44+ ' )' : ' (' ,
45+ ' ]' : ' [' ,
46+ ' }' : ' {' ,
47+ }
48+
49+ stack = list ()
50+ for ch in s:
51+ if ch in pairs:
52+ if not stack or stack[- 1 ] != pairs[ch]:
53+ return False
54+ stack.pop()
55+ else :
56+ stack.append(ch)
57+ return not stack
58+ ```
You can’t perform that action at this time.
0 commit comments