怀来住房和城乡建设局网站,网站建设论文伯乐在线,网站开发商换了,wordpress tag 优化算法学习——LeetCode力扣栈与队列篇1 232. 用栈实现队列
232. 用栈实现队列 - 力扣#xff08;LeetCode#xff09;
描述
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作#xff08;push、pop、peek、empty#xff09;#xff1a;
实现 MyQu…算法学习——LeetCode力扣栈与队列篇1 232. 用栈实现队列
232. 用栈实现队列 - 力扣LeetCode
描述
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作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双端队列来模拟一个栈只要是标准的栈操作即可。
示例
示例 1
输入 [“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”] [[], [1], [2], [], [], []] 输出 [null, null, null, 1, 1, false]
解释
MyQueue myQueue new MyQueue();myQueue.push(1); // queue is: [1]myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)myQueue.peek(); // return 1myQueue.pop(); // return 1, queue is [2]myQueue.empty(); // return false
提示
1 x 9最多调用 100 次 push、pop、peek 和 empty假设所有操作都是有效的 例如一个空的队列不会调用 pop 或者 peek 操作
进阶
你能否实现每个操作均摊时间复杂度为 O(1) 的队列换句话说执行 n 个操作的总时间复杂度为 O(n) 即使其中一个操作可能花费较长时间。
代码解析
class MyQueue {
public:MyQueue() {}void push(int x) {s1.push(x);}int pop() {if(s2.empty() ! 1) {int result s2.top();s2.pop();return result;}else{while(s1.empty() ! 1){int tmp s1.top();s1.pop();s2.push(tmp);}int result s2.top();s2.pop();return result;} }int peek() {if(s2.empty() 1){while(s1.empty() ! 1){int tmp s1.top();s1.pop();s2.push(tmp);}}return s2.top();}bool empty() {if(s1.empty() 1 s2.empty() 1)return true;else return false;}
public:stackint s1,s2;
};/*** 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. 用队列实现栈
225. 用队列实现栈 - 力扣LeetCode
描述
请你仅使用两个队列实现一个后入先出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双端队列来模拟一个队列 , 只要是标准的队列操作即可。
示例
输入 [“MyStack”, “push”, “push”, “top”, “pop”, “empty”] [[], [1], [2], [], [], []] 输出 [null, null, null, 2, 2, false]
解释 MyStack myStack new MyStack(); myStack.push(1); myStack.push(2); myStack.top(); // 返回 2 myStack.pop(); // 返回 2 myStack.empty(); // 返回 False
提示
1 x 9最多调用100 次 push、pop、top 和 empty每次调用 pop 和 top 都保证栈不为空
进阶
你能否仅用一个队列来实现栈。
代码解析
两个队列实现栈
#include iostream
#includestring
#includevector
#includeunordered_map
#include algorithm
#includemap
#includestack
#include queue
using namespace std;class MyStack {
public:MyStack() {}void push(int x) {que_1.push(x);}int pop() {int result 0;while (que_1.empty() 0){result que_1.front();que_2.push(que_1.front());que_1.pop();}int num que_2.size() - 1;for (int i 0; i num ; i){que_1.push(que_2.front());que_2.pop();}que_2.pop();return result;}int top() {int result 0;while (que_1.empty() 0){result que_1.front();que_2.push(que_1.front());que_1.pop();}while (que_2.empty() 0){que_1.push(que_2.front());que_2.pop();}return result;}bool empty() {if (que_1.empty() 1)return 1;else return 0;}
public:queueint que_1;queueint que_2;
};int main() {MyStack obj;obj.push(1);obj.push(2);obj.push(3);cout obj.pop() endl;cout obj.pop() endl;cout obj.pop() endl;cout obj.empty() endl;return 0;}
一个队列实现栈 class MyStack {
public:MyStack() {}void push(int x) {que_1.push(x);}int pop() {int result 0;int size que_1.size() -1 ;for (int i 0; i size; i){result que_1.front();que_1.pop();que_1.push(result);}result que_1.front();que_1.pop();return result;}int top() {int result 0;int size que_1.size() ;for (int i 0; i size; i){result que_1.front();que_1.pop();que_1.push(result);}return result;}bool empty() {if (que_1.empty() 1)return 1;else return 0;}
public:queueint que_1;};
20. 有效的括号
20. 有效的括号 - 力扣LeetCode
描述
给定一个只包括 ‘(’‘)’‘{’‘}’‘[’‘]’ 的字符串 s 判断字符串是否有效。
有效字符串需满足
左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 每个右括号都有一个对应的相同类型的左括号。
示例
示例 1
输入s “()” 输出true
示例 2
输入s “()[]{}” 输出true
示例 3
输入s “(]” 输出false
提示
1 s.length 104s 仅由括号 ‘()[]{}’ 组成
代码解析
遇到左括号压栈左括号
class Solution {
public:bool checkout( char a , char b ) //检查括号是否匹配{if (a ( b )) return 1;else if (a [ b ]) return 1;else if (a { b }) return 1;else return 0;}bool isValid(string s) {stackchar stack_s;stack_s.push(1);//防止空栈时top函数报错提前压栈stack_s.push(s[0]);for (int i 1; i s.size(); i){if (checkout(stack_s.top(), s[i]) 0) //不匹配压栈{stack_s.push(s[i]);}else//匹配弹栈{stack_s.pop();}}stack_s.pop();//弹出之前压的1return stack_s.empty();}
};
遇到左括号压栈右括号
class Solution {
public:bool isValid(string s) {if (s.size() % 2 ! 0) return false; // 如果s的长度为奇数一定不符合要求stackchar 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(]);// 第三种情况遍历字符串匹配的过程中栈已经为空了没有匹配的字符了说明右括号没有找到对应的左括号 return false// 第二种情况遍历字符串匹配的过程中发现栈里没有我们要匹配的字符。所以return falseelse if (st.empty() || st.top() ! s[i]) return false;else st.pop(); // st.top() 与 s[i]相等栈弹出元素}// 第一种情况此时我们已经遍历完了字符串但是栈不为空说明有相应的左括号没有右括号来匹配所以return false否则就return truereturn st.empty();}
};
1047. 删除字符串中的所有相邻重复项
1047. 删除字符串中的所有相邻重复项 - 力扣LeetCode
描述
给出由小写字母组成的字符串 S重复项删除操作会选择两个相邻且相同的字母并删除它们。
在 S 上反复执行重复项删除操作直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
示例
输入“abbaca” 输出“ca” 解释 例如在 “abbaca” 中我们可以删除 “bb” 由于两字母相邻且相同这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 “aaca”其中又只有 “aa” 可以执行重复项删除操作所以最后的字符串为 “ca”。
提示
1 S.length 20000S 仅由小写英文字母组成。
代码解析
返回原本的字符串
class Solution {
public:string removeDuplicates(string s) {stackchar stack_s;stack_s.push(1);stack_s.push(s[0]);for (int i 1; i s.size(); i){if (stack_s.top() ! s[i]){stack_s.push(s[i]);}else{stack_s.pop();}}s.erase(0, s.size());int num stack_s.size()-1;for(int i 0 ;i num; i){ s.insert(0,1,stack_s.top());//插入函数复杂度高每一个点都执行浪费时间stack_s.pop();}return s;}
};
返回另一个串
class Solution {
public:string removeDuplicates(string s){stackchar stack_s;stack_s.push(1);stack_s.push(s[0]);for (int i 1; i s.size(); i){if (stack_s.top() ! s[i]){stack_s.push(s[i]);}else{stack_s.pop();}}string result ;int num stack_s.size()-1;for(int i 0 ; i num; i){ result stack_s.top();//倒叙插入然后反转stack_s.pop();}reverse (result.begin(), result.end());//反转一次节省时间return result;} };