File tree Expand file tree Collapse file tree 4 files changed +35
-62
lines changed Expand file tree Collapse file tree 4 files changed +35
-62
lines changed Original file line number Diff line number Diff line change 87
87
88
88
* 编程语言
89
89
* [ C++面试&C++学习指南知识点整理] ( https://github.com/youngyangyang04/TechCPP )
90
+ * 项目
91
+ * [ 基于跳表的轻量级KV存储引擎] ( https://github.com/youngyangyang04/Skiplist-CPP )
90
92
91
93
* 编程素养
92
94
* [ 看了这么多代码,谈一谈代码风格!] ( ./problems/前序/代码风格.md )
Original file line number Diff line number Diff line change @@ -22,24 +22,24 @@ https://leetcode-cn.com/problems/valid-parentheses/
22
22
* 注意空字符串可被认为是有效字符串。
23
23
24
24
示例 1:
25
- 输入: "()"
26
- 输出: true
25
+ * 输入: "()"
26
+ * 输出: true
27
27
28
28
示例 2:
29
- 输入: "()[ ] {}"
30
- 输出: true
29
+ * 输入: "()[ ] {}"
30
+ * 输出: true
31
31
32
32
示例 3:
33
- 输入: "(] "
34
- 输出: false
33
+ * 输入: "(] "
34
+ * 输出: false
35
35
36
36
示例 4:
37
- 输入: "([ )] "
38
- 输出: false
37
+ * 输入: "([ )] "
38
+ * 输出: false
39
39
40
40
示例 5:
41
- 输入: "{[ ] }"
42
- 输出: true
41
+ * 输入: "{[ ] }"
42
+ * 输出: true
43
43
44
44
# 思路
45
45
@@ -90,7 +90,7 @@ cd a/b/c/../../
90
90
91
91
动画如下:
92
92
93
- ![ 20.有效括号] ( https://code-thinking.cdn.bcebos.com/gifs/20.%E6%9C%89%E6%95%88%E6%8B%AC%E5%8F%B7 .gif )
93
+ ![ 20.有效括号] ( https://code-thinking.cdn.bcebos.com/gifs/20.有效括号 .gif )
94
94
95
95
96
96
第一种情况:已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false
@@ -130,10 +130,6 @@ public:
130
130
技巧性的东西没有固定的学习方法,还是要多看多练,自己总灵活运用了。
131
131
132
132
133
-
134
-
135
-
136
-
137
133
## 其他语言版本
138
134
139
135
@@ -162,33 +158,6 @@ class Solution {
162
158
return deque.isEmpty();
163
159
}
164
160
}
165
- // 方法2
166
- class Solution {
167
- public boolean isValid(String s) {
168
-
169
- Stack<Character> stack = new Stack<>();
170
- Map<Character, Character> map = new HashMap<Character, Character>() {
171
- {
172
- put('}', '{');
173
- put(']', '[');
174
- put(')', '(');
175
- }
176
- };
177
-
178
- for (Character c : s.toCharArray()) { // 顺序读取字符
179
- if (!stack.isEmpty() && map.containsKey(c)) { // 是右括号 && 栈不为空
180
- if (stack.peek() == map.get(c)) { // 取其对应的左括号直接和栈顶比
181
- stack.pop(); // 相同则抵消,出栈
182
- } else {
183
- return false; // 不同则直接返回
184
- }
185
- } else {
186
- stack.push(c); // 左括号,直接入栈
187
- }
188
- }
189
- return stack.isEmpty(); // 看左右是否抵消完
190
- }
191
- }
192
161
```
193
162
194
163
Python:
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ https://leetcode-cn.com/problems/implement-stack-using-queues/
34
34
35
35
有的同学可能疑惑这种题目有什么实际工程意义,** 其实很多算法题目主要是对知识点的考察和教学意义远大于其工程实践的意义,所以面试题也是这样!**
36
36
37
- 刚刚做过[ 栈与队列:我用栈来实现队列怎么样?] ( https://mp.weixin.qq.com/s/P6tupDwRFi6Ay-L7DT4NVg ) 的同学可能依然想着用一个输入队列,一个输出队列,就可以模拟栈的功能,仔细想一下还真不行!
37
+ 刚刚做过[ 栈与队列:我用栈来实现队列怎么样?] ( https://mp.weixin.qq.com/s/Cj6R0qu8rFA7Et9V_ZMjCA ) 的同学可能依然想着用一个输入队列,一个输出队列,就可以模拟栈的功能,仔细想一下还真不行!
38
38
39
39
** 队列模拟栈,其实一个队列就够了** ,那么我们先说一说两个队列来实现栈的思路。
40
40
@@ -46,18 +46,21 @@ https://leetcode-cn.com/problems/implement-stack-using-queues/
46
46
47
47
如下面动画所示,** 用两个队列que1和que2实现队列的功能,que2其实完全就是一个备份的作用** ,把que1最后面的元素以外的元素都备份到que2,然后弹出最后面的元素,再把其他元素从que2导回que1。
48
48
49
- 模拟的队列执行语句如下:
50
- queue.push(1);
51
- queue.push(2);
52
- queue.pop(); // 注意弹出的操作
53
- queue.push(3);
54
- queue.push(4);
55
- queue.pop(); // 注意弹出的操作
56
- queue.pop();
57
- queue.pop();
58
- queue.empty();
49
+ 模拟的队列执行语句如下:
59
50
60
- ![ 225.用队列实现栈] ( https://code-thinking.cdn.bcebos.com/gifs/225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.gif )
51
+ ```
52
+ queue.push(1);
53
+ queue.push(2);
54
+ queue.pop(); // 注意弹出的操作
55
+ queue.push(3);
56
+ queue.push(4);
57
+ queue.pop(); // 注意弹出的操作
58
+ queue.pop();
59
+ queue.pop();
60
+ queue.empty();
61
+ ```
62
+
63
+ ![ 225.用队列实现栈] ( https://code-thinking.cdn.bcebos.com/gifs/225.用队列实现栈.gif )
61
64
62
65
详细如代码注释所示:
63
66
@@ -152,7 +155,7 @@ public:
152
155
};
153
156
```
154
157
155
- ## 其他语言版本
158
+ # 其他语言版本
156
159
157
160
Java:
158
161
Original file line number Diff line number Diff line change @@ -23,15 +23,14 @@ https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
23
23
24
24
25
25
示例:
26
- 输入:"abbaca"
27
- 输出:"ca"
28
- 解释:
29
- 例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
26
+ * 输入:"abbaca"
27
+ * 输出:"ca"
28
+ * 解释:例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
30
29
31
30
32
31
提示:
33
- 1 <= S.length <= 20000
34
- S 仅由小写英文字母组成。
32
+ * 1 <= S.length <= 20000
33
+ * S 仅由小写英文字母组成。
35
34
36
35
# 思路
37
36
@@ -64,7 +63,7 @@ S 仅由小写英文字母组成。
64
63
65
64
如动画所示:
66
65
67
- ![ 1047.删除字符串中的所有相邻重复项] ( https://code-thinking.cdn.bcebos.com/gifs/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9 .gif )
66
+ ![ 1047.删除字符串中的所有相邻重复项] ( https://code-thinking.cdn.bcebos.com/gifs/1047.删除字符串中的所有相邻重复项 .gif )
68
67
69
68
从栈中弹出剩余元素,此时是字符串ac,因为从栈里弹出的元素是倒叙的,所以在对字符串进行反转一下,就得到了最终的结果。
70
69
You can’t perform that action at this time.
0 commit comments