232.用栈实现队列
https://leetcode.cn/problems/implement-queue-using-stacks/
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾
int pop()
从队列的开头移除并返回元素
int peek()
返回队列开头的元素
boolean empty()
如果队列为空,返回 true ;否则,返回 false
说明:
你 只能 使用标准的栈操作 —— 也就是只有 push to top
, peek/pop from top
, size
, 和 is empty
操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
_stk1.push(x);
}
int pop() {
if (_stk1.empty()) return -1;
while (!_stk1.empty()) {
_stk2.push(_stk1.top());
_stk1.pop();
}
int ans = _stk2.top();
_stk2.pop();
while (!_stk2.empty()) {
_stk1.push(_stk2.top());
_stk2.pop();
}
return ans;
}
int peek() {
while (!_stk1.empty()) {
_stk2.push(_stk1.top());
_stk1.pop();
}
int ans = _stk2.top();
while (!_stk2.empty()) {
_stk1.push(_stk2.top());
_stk2.pop();
}
return ans;
}
bool empty() {
if (_stk1.empty()) {
return true;
}
else {
return false;
}
}
private:
stack<int> _stk1;
stack<int> _stk2;
};
/**
* 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();
*/
学会复用
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
_stk1.push(x);
}
int pop() {
if (_stk2.empty()) {
if (_stk1.empty()) {
return -1;
}
while (!_stk1.empty()) {
_stk2.push(_stk1.top());
_stk1.pop();
}
}
int ans = _stk2.top();
_stk2.pop();
return ans;
}
int peek() {
int ans = this->pop();
_stk2.push(ans);
return ans;
}
bool empty() {
return _stk1.empty() && _stk2.empty();
}
private:
stack<int> _stk1;
stack<int> _stk2;
};
/**
* 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();
*/
225. 用队列实现栈
https://leetcode.cn/problems/implement-stack-using-queues/
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack
类:
void push(int x)
将元素 x 压入栈顶。
int pop()
移除并返回栈顶元素。
int top()
返回栈顶元素。
boolean empty()
如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的标准操作 —— 也就是 push to back
、peek/pop from front
、size
和 is empty
这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
两个队列实现
class MyStack {
public:
MyStack() {
}
void push(int x) {
_que1.push(x);
}
int pop() {
if (_que1.empty()) {
return -1;
}
while (_que1.size() > 1) {
_que2.push(_que1.front());
_que1.pop();
}
int ans = _que1.front();
_que1.pop();
while (!_que2.empty()) {
_que1.push(_que2.front());
_que2.pop();
}
return ans;
}
int top() {
return _que1.back();
}
bool empty() {
return _que1.empty();
}
private:
queue<int> _que1;
queue<int> _que2;
};
/**
* 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();
*/
一个队列实现
pop元素时,将_que1的头部的元素(除了最后一个元素外),重新添加到队尾
class MyStack {
public:
MyStack() {
}
void push(int x) {
_que1.push(x);
}
int pop() {
if (_que1.empty()) {
return -1;
}
int len = _que1.size();
while (--len) {
_que1.push(_que1.front());
_que1.pop();
}
int ans = _que1.front();
_que1.pop();
return ans;
}
int top() {
return _que1.back();
}
bool empty() {
return _que1.empty();
}
private:
queue<int> _que1;
};
/**
* 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();
*/
20. 有效的括号
https://leetcode.cn/problems/valid-parentheses/
给定一个只包括 '('
,')'
,'{'
,'}'
,'['
,']'
的字符串 s
,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号。
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for (char c : s) {
if (!stk.empty()) {
if ((c == ')' && stk.top() == '(') ||
(c == ']' && stk.top() == '[') ||
(c == '}' && stk.top() == '{')) {
stk.pop();
continue;
}
}
stk.push(c);
}
return stk.empty();
}
};
1047. 删除字符串中的所有相邻重复项
https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
示例:
输入:"abbaca"
输出:"ca"
解释:
例如,在 "abbaca"
中,我们可以删除 "bb"
由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca"
,其中又只有 "aa"
可以执行重复项删除操作,所以最后的字符串为 "ca"
。
栈的经典应用。
class Solution {
public:
string removeDuplicates(string s) {
stack<char> stk;
for (char c : s) {
if (!stk.empty()) {
if (c == stk.top()) {
stk.pop();
continue;
}
}
stk.push(c);
}
s.erase(s.begin(), s.end());
while (!stk.empty()) {
s += stk.top();
stk.pop();
}
reverse(s.begin(),s.end());
return s;
}
};
字符串做栈
class Solution {
public:
string removeDuplicates(string s) {
string res;
for (char c : s) {
if (!res.empty() && c == res.back()) {
res.pop_back();
}
else {
res.push_back(c);
}
}
return res;
}
};