232.用栈实现队列(简单)
题目:
使用栈实现队列的下列操作:
- push(x) – 将一个元素放入队列的尾部。
- pop() – 从队列首部移除元素。
- peek() – 返回队列首部的元素。
- empty() – 返回队列是否为空。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
- 队列的特征先进先出, 而栈的特征是后进先出。
- 为了用栈来实现队列的特性,就需要使用两个栈A,B, A栈用来执行队列的push 操作,而B栈用来执行队列的pop top peek操作。
- A栈的push 操作需要注意是否将B栈的所有元素全部出栈并且压入A栈, 只有B栈为空,此时才能将新元素压入A栈中,保证队列的顺序性
- B栈的pop 操作同时需要注意是否将A栈的所有元素全部出栈并且压入B栈,只有A栈为空,此时才能将队首元素出栈, 保证队列的顺序性
- 队列的判空基于两个栈是否同时为空
class MyQueue {
private:
stack<int> sta1; // used for operation-push
stack<int> sta2; // used for operation pop, top and peek
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
while(!sta2.empty()){
sta1.push(sta2.top());
sta2.pop();
}
sta1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int num;
while(!sta1.empty()){
sta2.push(sta1.top());
sta1.pop();
}
num = sta2.top();
sta2.pop();
return num;
}
/** Get the front element. */
int peek() {
while(!sta1.empty()){
sta2.push(sta1.top());
sta1.pop();
}
return sta2.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return sta1.empty() && sta2.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();
*/
225.用队列实现栈(简单)
题目:
使用队列实现栈的下列操作:
- push(x) – 元素 x 入栈
- pop() – 移除栈顶元素
- top() – 获取栈顶元素
- empty() – 返回栈是否为空
这道题就没有任何难度了,直接使用STL中的 deque 相关操作来完成
- 将双端队列的右端作为栈顶,左段作为栈底
- 栈的push 操作为 deque 的push_back
- 栈的pop 操作为 deque 的pop_back
- 栈的top 操作为 deque的back
- 栈的判空为 deque的empty
class MyStack {
private:
deque<int> que;
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push_back(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int num;
if(!empty()){
num = que.back();
que.pop_back();
}
return num;
}
/** Get the top element. */
int top() {
return que.back();
}
/** Returns whether the stack is empty. */
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();
*/
844.比较包含退格的字符串
题目:
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果。 # 代表退格字符。
示例 1:
输入:S = "ab#c", T = "ad#c"
输出:true
解释:S 和 T 都会变成 “ac”。
示例 2:
输入:S = "ab##", T = "c#d#"
输出:true
解释:S 和 T 都会变成 “”。
示例 3:
输入:S = "a##c", T = "#a#c"
输出:true
解释:S 和 T 都会变成 “c”。
示例 4:
输入:S = "a#c", T = "b"
输出:false
解释:S 会变成 “c”,但 T 仍然是 “b”。
直观的方法还是用栈,遍历字符串,当遇到非’#‘字符时将该字符压入栈中,遇到’#'字符则出栈一次,最终比较两个栈内存储字符是否相同。
- 使用一个stS 栈来存储S 的非’#‘字符, 使用stT 来存储T 的非’#'字符
- 遍历S字符串,当字符不为’#‘时,将该字符压入stS中,当字符为’#'时将stS出栈一次
- 遍历T字符串,当字符不为’#‘时,将该字符压入stT中,当字符为’#'时将stT出栈一次
- 比较stS 与 stT 栈的内容
class Solution {
public:
bool backspaceCompare(string S, string T) {
stack<char> stS;
stack<char> stT;
for(int i = 0; i < S.length(); i++){
if(S[i] == '#'){
if(S.empty()){
continue;
}
else{
stS.pop();
}
}
else{
stS.push(S[i]);
}
}
for(int i = 0; i < T.length(); i++){
if(T[i] == '#'){
if(T.empty()){
continue;
}
else{
stT.pop();
}
}
else{
stT.push(T[i]);
}
}
if(stS.size() != stT.size()){
return false;
}
for(int i = 0 ;i < stS.size() ;i++)
{
if(stS.top() == stT.top())
{
stS.pop();
stT.pop();
}
else{
return false;
}
}
return true;
}
};
20.有效的括号(简单)
题目:
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: "()"
输出: true
示例 2:
输入: "()[]{}"
输出: true
示例 3:
输入: "(]"
输出: false
示例 4:
输入: "{[]}"
输出: true
在逆波兰表达式的方法中,栈的重要性不言而喻。因此此题若通过栈的方法来解决异常容易。
首先设立一个字符栈st, 遍历整个字符串,若字符为** ( [ {**其中之一时将字符入栈,若字符不为这三个字符则将st栈顶元素出栈,查看栈顶元素与该字符是否匹配。
- 设立字符栈st,遍历字符串
- 若当前字符为 ( [ 或者 { 其中之一, 将当前字符入栈
- 若当前字符不为 ( [ 或者 { 其中之一,则判断栈是否为空,栈非空则弹出栈顶字符,若栈为空则直接返回 false
- 查看栈顶字符与当前字符查看是否匹配,若不匹配则直接返回false
- 遍历完字符串查看栈是否为空,不为空则返回false
class Solution {
public:
bool isValid(string s) {
stack<char> st;
int len = s.length();
for(int i = 0; i < len; i++){
if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
st.push(s[i]);
}
else{
if(st.empty()){
return false;
}
char ch = st.top(); st.pop();
if(ch == '(' && s[i] != ')'){
return false;
}
else if(ch == '[' && s[i] != ']'){
return false;
}
else if(ch == '{' && s[i] != '}'){
return false;
}
else;
}
}
if(!st.empty()){
return false;
}
return true;
}
};