Skip to content

Commit 54ba14f

Browse files
committed
添加题150,394,94
1 parent c673840 commit 54ba14f

File tree

2 files changed

+102
-5
lines changed

2 files changed

+102
-5
lines changed

数据结构篇/栈和队列.md

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
### 简介
44

5-
栈的特点是后入先出,根据这个特点可以临时保存一些数据,之后用到依次再弹出来,常用于 DFS 深度搜索
5+
****的特点是后入先出,根据这个特点可以临时保存一些数据,之后用到依次再弹出来,常用于 DFS 深度搜索
66

7-
队列一般常用于 BFS 广度搜索,类似一层一层的搜索。
7+
**队列**一般常用于 BFS 广度搜索,类似一层一层的搜索。
88

99
### Stack 栈
1010

11-
##### 155.最小栈 [min-stack](https://leetcode-cn.com/problems/min-stack/)
11+
##### [155.最小栈](https://leetcode-cn.com/problems/min-stack/)
1212

1313
> 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
1414
@@ -55,7 +55,104 @@ MinStack.prototype.getMin = function() {
5555
};
5656
```
5757

58-
##### 150.逆波兰表达式求值 [evaluate-reverse-polish-notation](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/)
58+
##### [150.逆波兰表达式求值 ](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/)
5959

60-
##### 133.克隆图 [clone-graph](https://leetcode-cn.com/problems/clone-graph/)
60+
```js
61+
var evalRPN = function (tokens) {
62+
const stack = []
63+
for (const item of tokens) {
64+
if (!isNaN(+item)) {
65+
stack.push(item)
66+
} else {
67+
let res = 0
68+
const top = parseInt(stack.pop()) //在栈中的位置靠上(被除数)
69+
const down = parseInt(stack.pop()) //靠下(除数)
70+
switch (item) {
71+
case '+':
72+
res = top + down
73+
break
74+
case '-':
75+
res = down - top
76+
break
77+
case '*':
78+
res = top * down
79+
break
80+
case '/':
81+
res = parseInt(down / top)
82+
break
83+
}
84+
stack.push(res)
85+
}
86+
}
87+
return stack[0]
88+
};
89+
```
90+
91+
##### [394. 字符串解码](https://leetcode-cn.com/problems/decode-string/)
92+
93+
给定一个经过编码的字符串,返回它解码后的字符串。
94+
95+
> 输入:s = "3[a]2[bc]"
96+
> 输出:"aaabcbc"
97+
98+
```js
99+
var decodeString = function (s) {
100+
const stack = []
101+
for (let i = 0; i < s.length; i++) {
102+
const char = s[i]
103+
if (char !== ']') {
104+
stack.push(char)
105+
} else {
106+
let str = ''
107+
while (stack.length > 0) {
108+
const temp = stack.pop()
109+
if (temp !== '[') {
110+
str = temp + str
111+
} else {
112+
let num = ''
113+
while (stack.length > 0) {
114+
const tempNum = stack.pop() // 处理类似100的数字,此时栈中['1','0','0']
115+
if (!isNaN(tempNum)) {
116+
num = tempNum + num
117+
} else {
118+
stack.push(tempNum)
119+
break
120+
}
121+
}
122+
str = str.repeat(+num)
123+
break
124+
}
125+
}
126+
stack.push(str)
127+
}
128+
}
129+
return stack.join('')
130+
}
131+
```
132+
133+
##### [94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
134+
135+
```js
136+
var inorderTraversal = function (root) { //递归写法
137+
if (root === null) {
138+
return []
139+
}
140+
const res = [];
141+
(function inOrder(root) {
142+
if (!root) {
143+
return
144+
}
145+
inOrder(root.left)
146+
res.push(root.val)
147+
inOrder(root.right)
148+
})(root);
149+
return res
150+
};
151+
```
152+
153+
```js
154+
155+
```
156+
157+
##### [133.克隆图](https://leetcode-cn.com/problems/clone-graph/)
61158

0 commit comments

Comments
 (0)