Leetcode 232 用栈实现队列
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
stack_1.push(x);
}
int pop() {
if(stack_2.empty()){
while(!stack_1.empty()){
stack_2.push(stack_1.top());
stack_1.pop();
}
}
int res = stack_2.top();
stack_2.pop();
return res;
}
int peek() {
if(stack_2.empty()){
while(!stack_1.empty()){
stack_2.push(stack_1.top());
stack_1.pop();
}
}
return stack_2.top();
}
bool empty() {
return stack_1.empty() && stack_2.empty();
}
private:
std::stack<int> stack_1;
std::stack<int> stack_2;
};
/**
* 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();
*/
学会减少重复代码,遇到两个相似功能时,避免把代码复制粘贴过来改,学会抽象代码,减少冗余。这里peek()函数需要用到类似pop()的功能,可以用this指针,避免再写一遍。
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
stack_1.push(x);
}
int pop() {
if(stack_2.empty()){
while(!stack_1.empty()){
stack_2.push(stack_1.top());
stack_1.pop();
}
}
int res = stack_2.top();
stack_2.pop();
return res;
}
int peek() {
int res = this -> pop();
stack_2.push(res);
return res;
}
bool empty() {
return stack_1.empty() && stack_2.empty();
}
private:
std::stack<int> stack_1;
std::stack<int> stack_2;
};
/**
* 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 225 用队列模拟栈
同样的模拟方法,这里只需要一个队列即可,出栈时只需要将队列的front元素移动到队列末尾。
class MyStack {
public:
std::queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
for(int i = que.size() - 1; i > 0; i--){
que.push(que.front());
que.pop();
}
int res = que.front();
que.pop();
return res;
}
int top() {
int res = this -> pop();
que.push(res);
return res;
}
bool empty() {
return que.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
Leetcode 20 有效的括号
刚开始枚举,结果发现没有考虑“{[]}”。官解提示用栈来解决:代码冗余很多。
class Solution {
public:
bool isValid(string s) {
std::stack<char> st;
for(int i = 0; i < s.size(); i++){
if(st.empty()){
st.push(s[i]);
continue;
}
if(st.top() == '('){
if(s[i] == ')'){
st.pop();
}else{
st.push(s[i]);
}
continue;
}
if(st.top() == '['){
if(s[i] == ']'){
st.pop();
}else{
st.push(s[i]);
}
continue;
}
if(st.top() == '{'){
if(s[i] == '}'){
st.pop();
}else{
st.push(s[i]);
}
continue;
}
}
return st.empty() ? true : false;
}
};
随后看了官解,在遍历s时,左括号入栈进行匹配时要区分三种情况,代码冗余多,而右括号入栈只需判断栈顶和s[i]是否相等,代码会简洁很多。
class Solution {
public:
bool isValid(string s) {
std::stack<char> 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() || s[i] != st.top()) {return false;}
else {st.pop();}
}
return st.empty();
}
};
Leetcode 1047 删除字符串中所有相邻重复项
同样使用栈来完成,当遍历到第i个元素时,如果和栈顶元素相同,则弹出当前栈顶,继续遍历i+1.
class Solution {
public:
string removeDuplicates(string s) {
stack<int> st;
for(int i = 0; i < s.size(); i++){
if(!st.empty() && s[i] == st.top()) {st.pop();}
else {st.push(s[i]);}
}
s.resize(st.size());
for(int i = s.size() - 1; i >= 0; i--){
s[i] = st.top();
st.pop();
}
return s;
}
};