代码随想录算法训练营第十一天|232.用栈实现队列、225.用队列实现栈、20.有效的括号、1047.删除字符串中的所有相邻重复项

232和225题一个思路 一起贴了
复制代码
 1 class MyQueue {
 2 public:
 3     MyQueue() {
 4         stack<int> in;
 5         stack<int> out;
 6     }
 7     
 8     void push(int x) {
 9         in.push(x);
10     }
11     
12     int pop() {
13         while (!in.empty()) {
14             out.push(in.top());
15             in.pop();
16         }
17         int ans = out.top();
18         out.pop();
19         while (!out.empty()) {
20             in.push(out.top());
21             out.pop();
22         }
23         return ans;
24     }
25     
26     int peek() {
27         while (!in.empty()) {
28             out.push(in.top());
29             in.pop();
30         }
31         int ans = out.top();
32         while (!out.empty()) {
33             in.push(out.top());
34             out.pop();
35         }
36         return ans;
37     }
38     
39     bool empty() {
40         if (in.empty()) {
41             return true;
42         }
43         return false;
44     }
45 private:
46     stack<int> in;
47     stack<int> out;
48 
49 };
复制代码
复制代码
 1 class MyStack {
 2 public:
 3     MyStack() {
 4         
 5     }
 6     
 7     void push(int x) {
 8         // 先将 x 入队到 queue2 中
 9         q2.push(x);
10         
11         // 将 queue1 中的所有元素依次转移到 queue2 中,确保 queue2 中的元素顺序是栈顺序
12         while (!q1.empty()) {
13             q2.push(q1.front());
14             q1.pop();
15         }
16         
17         // 交换 queue1 和 queue2,使得 queue1 始终是主要存储栈元素的队列
18         std::swap(q1, q2);
19     }
20     
21     int pop() {
22         // 直接从 queue1 中弹出栈顶元素
23         int top_elem = q1.front();
24         q1.pop();
25         return top_elem;
26     }
27     
28     int top() {
29         // 返回 queue1 的队首元素,即栈顶元素
30         return q1.front();
31     }
32     
33     bool empty() {
34         // 判断 queue1 是否为空
35         return q1.empty();
36     }
37 
38 private:
39     std::queue<int> q1;
40     std::queue<int> q2;
41 };
复制代码

20题就是简单的符号匹配

复制代码
 1 class Solution {
 2 private:
 3     vector<char>Stack;
 4 public:
 5     bool isValid(string s) {
 6         for(int i=0;i<s.size();i++){
 7             if(s[i]=='('){
 8                 Stack.push_back('(');
 9             }
10             else if(s[i]==')'){
11                 if(Stack.empty()){
12                     return false;
13                 }
14                 if(Stack.back()!='('){
15                     return false;
16                 }
17                 Stack.pop_back();
18             }
19             else if(s[i]=='['){
20                 Stack.push_back('[');
21             }
22             else if(s[i]==']'){
23                 if(Stack.empty()){
24                     return false;
25                 }
26                 if(Stack.back()!='['){
27                     return false;
28                 }
29                 Stack.pop_back();
30             }
31             else if(s[i]=='{'){
32                 Stack.push_back('{');
33             }
34             else if(s[i]=='}'){
35                 if(Stack.empty()){
36                     return false;
37                 }
38                 if(Stack.back()!='{'){
39                     return false;
40                 }
41                 Stack.pop_back();
42             }
43         }
44         if(Stack.size()!=0){
45             return false;
46         }
47         return true;
48     }
49 };
复制代码

1047题倒是有点意思 但是我想多了 额外做了一个去重的操作 太呆了 其实也就是变种的符号匹配

复制代码
 1 // //变种的符号匹配
 2 // class Solution {
 3 // public:
 4 //     string removeDuplicates(string s) {
 5 //         // 定义两个栈 tmp1 和 tmp2
 6 //         stack<char> tmp1;
 7 //         stack<char> tmp2;
 8 //         int len = s.size(); // 获取字符串 s 的长度
 9 //         int i = 0; // 初始化索引 i
10 //         bool flag; // 定义一个标志位用于控制内层循环
11 
12 //         // 外层循环遍历字符串 s
13 //         while (i < len) {
14 //             // 如果 tmp1 不为空且当前字符 s[i] 与 tmp1 的栈顶元素相同
15 //             if (!tmp1.empty() && s[i] == tmp1.top()) {
16 //                 tmp1.pop(); // 弹出 tmp1 的栈顶元素
17 //                 i++; // 继续处理下一个字符
18 //                 flag = true; // 将标志位设置为 true
19 
20 //                 // 内层循环处理 tmp1 中相邻且相同的元素
21 //                 while (flag && !tmp1.empty()) {
22 //                     tmp2.push(tmp1.top()); // 将 tmp1 的栈顶元素推入 tmp2
23 //                     tmp1.pop(); // 弹出 tmp1 的栈顶元素
24 
25 //                     // 如果 tmp2 的栈顶元素和 tmp1 的新栈顶元素相同
26 //                     if (!tmp1.empty() && tmp2.top() == tmp1.top()) {
27 //                         tmp1.pop(); // 弹出 tmp1 的栈顶元素
28 //                         tmp2.pop(); // 弹出 tmp2 的栈顶元素
29 //                         flag = true; // 继续检查下一个元素
30 //                     } else {
31 //                         // 如果不相同,则将 tmp2 的栈顶元素重新推回 tmp1
32 //                         tmp1.push(tmp2.top());
33 //                         tmp2.pop();
34 //                         flag = false; // 结束内层循环
35 //                     }
36 //                 }
37 //             }
38 //             // 如果 tmp1 为空或者当前字符 s[i] 与 tmp1 的栈顶元素不同
39 //             else if (tmp1.empty() || s[i] != tmp1.top()) {
40 //                 tmp1.push(s[i]); // 将当前字符推入 tmp1
41 //                 i++; // 继续处理下一个字符
42 //             }
43 //         }
44 
45 //         // 将 tmp1 中的所有元素依次弹出,压入 tmp2,逆转顺序
46 //         while (!tmp1.empty()) {
47 //             tmp2.push(tmp1.top());
48 //             tmp1.pop();
49 //         }
50 
51 //         // 构建结果字符串
52 //         string result;
53 //         while (!tmp2.empty()) {
54 //             result += tmp2.top(); // 将 tmp2 的栈顶元素添加到结果字符串
55 //             tmp2.pop();
56 //         }
57 
58 //         // 返回最终的结果字符串
59 //         return result;
60 //     }
61 // };
62 class Solution {
63 public:
64     string removeDuplicates(string s) {
65         stack<char> tmp; // 定义一个栈 tmp
66         
67         for (char c : s) { // 遍历字符串 s
68             if (!tmp.empty() && tmp.top() == c) {
69                 tmp.pop(); // 如果当前字符与栈顶元素相等,则弹出栈顶元素
70             } else {
71                 tmp.push(c); // 否则将当前字符压入栈中
72             }
73         }
74         
75         // 构建结果字符串
76         string result;
77         while (!tmp.empty()) {
78             result += tmp.top(); // 将栈顶元素添加到结果字符串
79             tmp.pop(); // 弹出栈顶元素
80         }
81         
82         // 因为栈是先进后出,所以需要反转字符串
83         reverse(result.begin(), result.end());
84         
85         return result; // 返回最终的结果字符串
86     }
87 };
复制代码

 

posted @   清源风起时  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示