
1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)
- class Solution {
- public:
- string removeDuplicates(string s) {
- string stack;
- for(char& ch:s){
- if(stack.size()>0&&ch==stack.back()){
- stack.pop_back();
- }else{
- stack.push_back(ch);
- }
- }
- return stack;
- }
- };
- class Solution {
- public:
- bool backspaceCompare(string s, string t) {
- string tmp_s;
- string tmp_t;
- for(char& ch:s){
- if(ch=='#'){
- if(tmp_s.size()>0)
- tmp_s.pop_back();
- }else{
- tmp_s.push_back(ch);
- }
- }
- for(char& ch:t){
- if(ch=='#'){
- if(tmp_t.size()>0)
- tmp_t.pop_back();
- }else{
- tmp_t.push_back(ch);
- }
- }
- if(tmp_s==tmp_t)
- return true;
- return false;
- }
- };
- class Solution {
- public:
- int calculate(string s) {
- vector<int> stack;//栈内不存在加减乘除符号,符号遍历时用op保存,如果有括号的话,需要另一个栈,然后加减符号可以用正负代替插入。
- int i=0; int n=s.size();
- char op='+';
- while(i
- if(s[i]==' ') i++;
- else if(0+'0'<=s[i]&&s[i]<=0+'9'){
- //提取数字
- int tmp=s[i++]-'0';
- while(0+'0'<=s[i]&&s[i]<=0+'9') tmp+=tmp*10+s[i++]-'0';
- cout<<"op: "<
- cout<
- if(op=='+') stack.push_back(tmp);
- else if(op=='-') stack.push_back(-tmp);
- else if(op=='*'){
- int tmp1=stack.back();//必须在push前先pop
- stack.pop_back();
- stack.push_back(tmp1*tmp);
- // stack.pop_back();
- }else if(op=='/'){
- int tmp1=stack.back();
- stack.pop_back();
- stack.push_back(tmp1/tmp);
- }
- for(int num:stack){cout<
" ";}cout< - }else {
- // cout<<"op: "<
- op=s[i++];
- }
- }
-
-
- int ret=0;
- for(int& num:stack){
- // cout<
- ret+=num;
- }
- return ret;
- }
- };
- class Solution {
- public:
- bool validateStackSequences(vector<int>& pushed, vector<int>& popped){
- stack<int> st;
- int i=0; int n=popped.size();
- for(int &val : pushed){
- st.push(val);
- while(st.size()&&st.top()==popped[i]){
- st.pop();
- i++;
- }
- }
- return st.size()==0;
- }
- };
-
相关阅读:
Mybatis 插件使用及源码分析
Vue前端框架08 Vue框架简介、VueAPI风格、模板语法、事件处理、数组变化侦测
高等数值计算方法学习笔记第5章【解线性方程组的直接方法】
web前端大作业--美团外卖1
istio gateway入口流量路由管控
SpringBoot SpringBoot 原理篇 2 自定义starter 2.4 使用属性配置设置功能参数【1】
求解 C++问题 求解 求解
uniapp视频播放功能
c++ 中 auto, auto & 和 const auto & 的区别
go select 使用总结
-
原文地址:https://blog.csdn.net/weixin_50470247/article/details/137973950