Skip to content

Commit dbd354d

Browse files
committed
feat: add cpp solutions to lcof problems: No. 30,31,32
1 parent 5b1d7f4 commit dbd354d

File tree

6 files changed

+155
-4
lines changed

6 files changed

+155
-4
lines changed

lcof/面试题30. 包含min函数的栈/README.md

+40-4
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ class MinStack {
7373
mins = new ArrayDeque<>();
7474
mins.push(Integer.MAX_VALUE);
7575
}
76-
76+
7777
public void push(int val) {
7878
s.push(val);
7979
mins.push(Math.min(mins.peek(), val));
8080
}
81-
81+
8282
public void pop() {
8383
s.pop();
8484
mins.pop();
8585
}
86-
86+
8787
public int top() {
8888
return s.peek();
8989
}
90-
90+
9191
public int min() {
9292
return mins.peek();
9393
}
@@ -158,6 +158,42 @@ MinStack.prototype.min = function () {
158158
*/
159159
```
160160

161+
### **C++**
162+
163+
```cpp
164+
class MinStack {
165+
private:
166+
stack<int> a, b;
167+
168+
public:
169+
/** initialize your data structure here. */
170+
MinStack() {
171+
}
172+
173+
void push(int x) {
174+
a.push(x);
175+
if (b.empty() || x <= b.top()) {
176+
b.push(x);
177+
}
178+
}
179+
180+
void pop() {
181+
if (a.top() == b.top()) {
182+
b.pop();
183+
}
184+
a.pop();
185+
}
186+
187+
int top() {
188+
return a.top();
189+
}
190+
191+
int min() {
192+
return b.top();
193+
}
194+
};
195+
```
196+
161197
### **...**
162198

163199
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class MinStack {
2+
private:
3+
stack<int> a, b;
4+
5+
public:
6+
/** initialize your data structure here. */
7+
MinStack() {
8+
}
9+
10+
void push(int x) {
11+
a.push(x);
12+
if (b.empty() || x <= b.top()) {
13+
b.push(x);
14+
}
15+
}
16+
17+
void pop() {
18+
if (a.top() == b.top()) {
19+
b.pop();
20+
}
21+
a.pop();
22+
}
23+
24+
int top() {
25+
return a.top();
26+
}
27+
28+
int min() {
29+
return b.top();
30+
}
31+
};

lcof/面试题31. 栈的压入、弹出序列/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ var validateStackSequences = function (pushed, popped) {
9898
};
9999
```
100100

101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
107+
stack<int> s;
108+
int i = 0;
109+
for (int x : pushed) {
110+
s.push(x);
111+
while (!s.empty() && s.top() == popped[i]) {
112+
s.pop();
113+
++i;
114+
}
115+
}
116+
return s.empty();
117+
}
118+
};
119+
```
120+
101121
### **...**
102122
103123
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
4+
stack<int> s;
5+
int i = 0;
6+
for (int x : pushed) {
7+
s.push(x);
8+
while (!s.empty() && s.top() == popped[i]) {
9+
s.pop();
10+
++i;
11+
}
12+
}
13+
return s.empty();
14+
}
15+
};

lcof/面试题32 - II. 从上到下打印二叉树 II/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,33 @@ func levelOrder(root *TreeNode) [][]int {
165165
}
166166
```
167167

168+
### **C++**
169+
170+
```cpp
171+
class Solution {
172+
public:
173+
vector<vector<int>> levelOrder(TreeNode* root) {
174+
vector<vector<int>> ans;
175+
if (root == NULL) return ans;
176+
queue<TreeNode*> q;
177+
q.push(root);
178+
while (!q.empty()) {
179+
int n = q.size();
180+
vector<int> v;
181+
for (int i = 0; i < n; ++i) {
182+
TreeNode* node = q.front();
183+
q.pop();
184+
v.emplace_back(node->val);
185+
if (node->left) q.push(node->left);
186+
if (node->right) q.push(node->right);
187+
}
188+
ans.emplace_back(v);
189+
}
190+
return ans;
191+
}
192+
};
193+
```
194+
168195
### **...**
169196
170197
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> levelOrder(TreeNode* root) {
4+
vector<vector<int>> ans;
5+
if (root == NULL) return ans;
6+
queue<TreeNode*> q;
7+
q.push(root);
8+
while (!q.empty()) {
9+
int n = q.size();
10+
vector<int> v;
11+
for (int i = 0; i < n; ++i) {
12+
TreeNode* node = q.front();
13+
q.pop();
14+
v.emplace_back(node->val);
15+
if (node->left) q.push(node->left);
16+
if (node->right) q.push(node->right);
17+
}
18+
ans.emplace_back(v);
19+
}
20+
return ans;
21+
}
22+
};

0 commit comments

Comments
 (0)