This commit is contained in:
youngyangyang04
2020-07-19 16:32:49 +08:00
parent e32c005967
commit 0e1f22bda2
7 changed files with 236 additions and 0 deletions

View File

@ -0,0 +1,26 @@
## 题目地址
https://leetcode-cn.com/problems/valid-parentheses/
## 思路
括号匹配是使用栈解决的经典问题
## C++代码
```
class Solution {
public:
bool isValid(string s) {
stack<int> st;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') st.push(')');
else if (s[i] == '{') st.push('}');
else if (s[i] == '[') st.push(']');
else if (st.empty() || st.top() != s[i]) return false;
else st.pop();
}
return st.empty();
}
};
```
> 更多精彩文章持续更新,可以微信搜索「 代码随想录」第一时间阅读,关注后有大量的学习资料和简历模板可以免费领取,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master )https://github.com/youngyangyang04/leetcode-master 已经收录欢迎starfork共同学习一起进步。

View File

@ -0,0 +1,50 @@
## 题目地址
https://leetcode-cn.com/problems/min-stack/
## 思路
有的同学一开始会把这道题目想简单了,用一个变量记录最小值不就得了,其实是如果要是弹出了这个最小值的话,我们还要记录次小值,所以需要一个辅助数组来记录次小值。
我这里使用数组来实现栈,在用一个数组来放当前栈里最小数值,同时使用辅助数组来记录
## C++代码
```
class MinStack {
public:
vector<int> vec;
vector<int> minVec;
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
vec.push_back(x);
if (minVec.size() == 0) {
minVec.push_back(x);
} else if (x <= minVec[minVec.size() - 1]) { // 这里一定是下小于等于,防止多个最小值的情况
minVec.push_back(x);
}
}
void pop() {
if (vec.size() == 0) { // 防止下面的操作会导致越界
return;
}
if (vec[vec.size() - 1] == minVec[minVec.size() - 1]) {
minVec.pop_back();
}
vec.pop_back();
}
int top() {
// 这里有越界的危险但是题目也没有说如果栈为空top()应该返回啥所以就默认测试用例没有上来直接top的用例了
return vec[vec.size() - 1];
}
int getMin() {
// 这里有越界的危险但是题目也没有说如果栈为空getMin()应该返回啥所以就默认测试用例没有上来直接getMin的用例了
return minVec[minVec.size() - 1];
}
};
```
> 笔者在先后在腾讯和百度从事技术研发多年利用工作之余重刷leetcode本文 [GitHub](https://github.com/youngyangyang04/leetcode-master )https://github.com/youngyangyang04/leetcode-master 已经收录欢迎starfork共同学习一起进步。

View File

@ -1,4 +1,9 @@
## 题目地址
## 思路
## C++代码
```

View File

@ -0,0 +1,59 @@
## 题目地址
https://leetcode-cn.com/problems/implement-stack-using-queues/
## 思路
用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质
建议大家题目编号: 225 和 232 一起做来做
详细如代码注释所示
## C++代码
```
class MyStack {
public:
queue<int> queIn;
queue<int> queOut;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
queIn.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = queIn.size();
size--;
while (size--) { // 将queIn 导入queOut但要留下最后一个元素
queOut.push(queIn.front());
queIn.pop();
}
int result = queIn.front(); // 留下的最后一个元素就是我们要返回的值
queIn.pop();
queIn = queOut; // 再将queOut赋值给queIn
while(!queOut.empty()) { // 清空queOut
queOut.pop();
}
return result;
}
/** Get the top element. */
int top() {
return queIn.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return queIn.empty();
}
};
```
> 笔者在先后在腾讯和百度从事技术研发多年利用工作之余重刷leetcode本文 [GitHub](https://github.com/youngyangyang04/leetcode-master )https://github.com/youngyangyang04/leetcode-master 已经收录欢迎starfork共同学习一起进步。

View File

@ -0,0 +1,59 @@
## 题目地址
https://leetcode-cn.com/problems/implement-queue-using-stacks/
## 思路
使用两个栈 一个输入栈,一个输出栈, 这里要注意输入栈和输出栈的关系每当pop的时候且输出栈为空就应该把输入站全部导入输出栈中
## C++代码
```
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if (stOut.empty()) { //只有当stOut为空的时候再从stIn里导入数据
while(!stIn.empty()) {
stOut.push(stIn.top()); // 从stIn导入数据知道stOut为空
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
/** Get the front element. */
int peek() {
int res = this->pop(); // 直接使用已有的pop函数
stOut.push(res); // 因为pop函数弹出了元素res所以再添加回去
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return stIn.empty() && stOut.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
```
> 笔者在先后在腾讯和百度从事技术研发多年利用工作之余重刷leetcode本文 [GitHub](https://github.com/youngyangyang04/leetcode-master )https://github.com/youngyangyang04/leetcode-master 已经收录欢迎starfork共同学习一起进步。

View File

@ -0,0 +1,32 @@
## 题目地址
https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
## 思路
这道题目就像是我们玩过的游戏对对碰, 可以把字符串放到与一个栈中,然后如果相同的话 栈就弹出,这样最后栈里剩下的元素都是相邻不相同的元素了
## C++代码
```
class Solution {
public:
string removeDuplicates(string S) {
stack<char> st;
for (char s : S) {
if (st.empty() || s != st.top()) {
st.push(s);
} else {
st.pop();
}
}
string result = "";
while(!st.empty()) {
result += st.top();
st.pop();
}
reverse(result.begin(), result.end()); // 此时字符串需要反转一下
return result;
}
};
```