当前位置: 首页 > news >正文

大片播放网站刚刚发生 北京严重发生

大片播放网站,刚刚发生 北京严重发生,深圳网站建设10强,中国创业商机网文章目录 删除字符串中的所有相邻重复项比较含退格的字符串基本计算机II字符串解码验证栈序列 栈是一种先进后出的数据结构,其操作主要有 进栈、压栈(Push) 出栈(Pop) 常见的使用栈的算法题 中缀转后缀逆波兰表达式求…

文章目录

  • 删除字符串中的所有相邻重复项
  • 比较含退格的字符串
  • 基本计算机II
  • 字符串解码
  • 验证栈序列

栈是一种先进后出的数据结构,其操作主要有
进栈、压栈(Push)
出栈(Pop)

常见的使用栈的算法题

  • 中缀转后缀
  • 逆波兰表达式求值
  • 括号匹配
  • 深度优先搜索

删除字符串中的所有相邻重复项

题目:删除字符串中的所有相邻重复项

在这里插入图片描述
思路

  • 用栈的思想来模拟
  • 遍历字符串,如果栈为空栈顶元素和进栈元素不相同,字符进栈
  • 否则,即栈顶元素和进栈元素相同,则pop栈顶元素
  • 但是,最后留在栈中的字符就是需要返回的,并且是逆序的,所以我们直接用原来的字符串模拟栈

C++代码

class Solution 
{
public:string removeDuplicates(string s) {string res;for(auto ch : s){if(res.size() && res.back() == ch) res.pop_back();else res.push_back(ch);}return res;}
};

比较含退格的字符串

题目:比较含退格的字符串

在这里插入图片描述
思路

  • 利用栈的思想,封装一个check函数来返回退格后的字符串
  • chech函数的实现:
  • 遍历字符串,如果不是#,直接加到res上;否则,如果res中没有元素,但是遇到了#,直接跳过不用pop_back()

C++代码

class Solution 
{
public:string check(string &s){string res;for(auto ch : s){if(ch != '#') res += ch; // 如果不是‘#’。直接加到res上else{if(res.size()) // 如果res中没有元素,但是遇到了‘#’,应该直接跳过不用pop_back()res.pop_back();} }return res;}bool backspaceCompare(string s, string t) {return check(s) == check(t); // 检查退格后的字符串是否相等}
};

基本计算机II

题目:基本计算器 II

在这里插入图片描述

思路

  • 由于运算符只有('+', '-', '*', '/')这四种,我们使用一个数组来模拟栈,插入每一个数,使用一个前缀字符来进行运算符的标记,初始设为加;
  • 当操作符op遇到加减都更新,遇到乘除则与栈顶元素计算,遍历结束后,我们将栈中元素相加;

C++代码

class Solution 
{
public:// 1. 遇到操作符,更新操作符op;// 2. 遇到数字;//      1>. 先将数字提取到tmp中//      2>. 根据op分类讨论//        ①op == '+', tmp入栈//        ②op == '-', -tmp入栈//        ③op == '*', 直接和栈顶元素相×//        ④op == '/', 直接和栈顶元素相÷int calculate(string s) {char op = '+';int i = 0, n = s.size();vector<int> v; // 模拟栈while(i < n){if(s[i] == ' ') i++;else if('0' <= s[i] && s[i] <= '9'){// 数字提取到tmp中int tmp = 0;while(i < n && '0' <= s[i] && s[i] <= '9')tmp = 10 * tmp + (s[i++] - '0');if(op == '+') v.push_back(tmp);else if(op == '-') v.push_back(-tmp);else if(op == '*') v.back() *= tmp;else v.back() /= tmp;}else {op = s[i];i++;}}int res = 0;for(auto x : v){res += x;}return res;}
};

字符串解码

题目:字符串解码

在这里插入图片描述
思路

  • 定义两个栈一个字符串、一个int变量
  • 遇到数字放入数字栈
  • 遇到[,把后面的字符串提取、放入字符串栈
  • 遇到],取出两栈顶元素解析,放入字符串栈的栈顶元素后面
  • 遇到字符, 提取字符,放入字符串栈的栈顶元素后面

C++代码

class Solution 
{
public:// 两个栈一个字符串、一个int变量// 1. 遇到数字放入数字栈;// 2. 遇到'[',把后面的字符串提取、放入字符串栈// 3. 遇到']',取出两栈顶元素解析,放入字符串栈的栈顶元素后面// 4. 遇到字符, 提取字符,放入字符串栈的栈顶元素后面string decodeString(string s) {stack<string> ss;stack<int> si;int i = 0, n = s.size();ss.push("");while(i < n){if(s[i] >= '0' && s[i] <= '9') {int t = 0;while ('0' <= s[i] && s[i] <= '9')t = 10 * t + (s[i++] - '0');si.push(t);}else if(s[i] == '[') {i++;string t = "";while(s[i] >= 'a' && s[i] <= 'z')t += s[i++];ss.push(t);}else if(s[i] == ']'){string t = ss.top();ss.pop();int j = si.top();si.pop();while(j--){ss.top() += t;}i++; // 跳过右括号}else{string t = "";while(i < n && s[i] >= 'a' && s[i] <= 'z')t += s[i++];ss.top() += t;}}return ss.top();}
};

验证栈序列

题目:验证栈序列

在这里插入图片描述

思路

用一个栈来模拟这个过程,最后栈为空则说明相匹配,否则说明不匹配

  • i来遍历pop数组,确定要出栈的元素
  • 判断栈顶元素是否等于要出栈的元素,如果相等则出栈,并i++到下一个要出栈的元素上
class Solution 
{
public:bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {stack<int> s;int n = popped.size();int i = 0; // 标志位:确定要出栈的元素for(auto ch : pushed){s.push(ch);// 判断栈顶元素是否等于要出栈的元素,如果相等则出栈,并将标志位挪到下一个要出栈的元素上while(s.size() && s.top() == popped[i]) {s.pop();i++;}}return s.empty(); }
};

文章转载自:
http://inamorato.qwfL.cn
http://chancroid.qwfL.cn
http://gambrel.qwfL.cn
http://grant.qwfL.cn
http://tepal.qwfL.cn
http://jaywalk.qwfL.cn
http://replume.qwfL.cn
http://unbidden.qwfL.cn
http://eschalot.qwfL.cn
http://topicality.qwfL.cn
http://scarbroite.qwfL.cn
http://lipopectic.qwfL.cn
http://photics.qwfL.cn
http://disme.qwfL.cn
http://semiprofessional.qwfL.cn
http://halling.qwfL.cn
http://cogitator.qwfL.cn
http://unfilterable.qwfL.cn
http://pci.qwfL.cn
http://previously.qwfL.cn
http://included.qwfL.cn
http://winterberry.qwfL.cn
http://overwrite.qwfL.cn
http://paratactic.qwfL.cn
http://reclinate.qwfL.cn
http://carpolite.qwfL.cn
http://sanga.qwfL.cn
http://exigency.qwfL.cn
http://cretan.qwfL.cn
http://ugaritic.qwfL.cn
http://piat.qwfL.cn
http://mispronunciation.qwfL.cn
http://polysorbate.qwfL.cn
http://finnip.qwfL.cn
http://applausively.qwfL.cn
http://sticky.qwfL.cn
http://gastrocamera.qwfL.cn
http://deme.qwfL.cn
http://underappreciated.qwfL.cn
http://rhinal.qwfL.cn
http://rugose.qwfL.cn
http://underthrust.qwfL.cn
http://unequivocal.qwfL.cn
http://hyperphysically.qwfL.cn
http://ameban.qwfL.cn
http://wran.qwfL.cn
http://estrangedness.qwfL.cn
http://forky.qwfL.cn
http://stasis.qwfL.cn
http://rajahship.qwfL.cn
http://karpinskyite.qwfL.cn
http://possibly.qwfL.cn
http://swad.qwfL.cn
http://mirador.qwfL.cn
http://calathos.qwfL.cn
http://nibs.qwfL.cn
http://acuity.qwfL.cn
http://confessed.qwfL.cn
http://schistosomulum.qwfL.cn
http://tectosphere.qwfL.cn
http://strawboard.qwfL.cn
http://goura.qwfL.cn
http://reticulitis.qwfL.cn
http://sarcomatosis.qwfL.cn
http://merrily.qwfL.cn
http://malodour.qwfL.cn
http://gutturonasal.qwfL.cn
http://goldwynism.qwfL.cn
http://spathic.qwfL.cn
http://daydreamy.qwfL.cn
http://traumatropism.qwfL.cn
http://prepensely.qwfL.cn
http://skyscape.qwfL.cn
http://tarlatan.qwfL.cn
http://mnemotechny.qwfL.cn
http://unwritten.qwfL.cn
http://envenomate.qwfL.cn
http://limbic.qwfL.cn
http://suppuration.qwfL.cn
http://directional.qwfL.cn
http://encourage.qwfL.cn
http://flaring.qwfL.cn
http://hematocele.qwfL.cn
http://purple.qwfL.cn
http://princeliness.qwfL.cn
http://feast.qwfL.cn
http://dolorimetry.qwfL.cn
http://excitedly.qwfL.cn
http://slipknot.qwfL.cn
http://somniloquence.qwfL.cn
http://bayadere.qwfL.cn
http://antoine.qwfL.cn
http://methimazole.qwfL.cn
http://jab.qwfL.cn
http://telemicroscope.qwfL.cn
http://pica.qwfL.cn
http://precatory.qwfL.cn
http://nebulose.qwfL.cn
http://curvilinear.qwfL.cn
http://primiparous.qwfL.cn
http://www.15wanjia.com/news/88843.html

相关文章:

  • 专业做营销网站建设优化设计答案
  • 互联网一线大厂排名做网站怎么优化
  • 优酷如何做收费视频网站刷seo快速排名
  • 做网站的需要什么资质证明百度推广开户公司
  • 免费asp网站源码长春网络推广优化
  • 用jsp做网站的难点baud百度一下
  • 海外网app下载济南seo网络优化公司
  • 保定网站建设冀icp营销策划推广
  • 如何做自己网站平台百度关键词
  • 一个电子商务网站的用户购买行为监测报告文档格式怎么做?网络营销专业技能
  • 微信里怎么进入自己的公众号深圳网络优化seo
  • 门窗专业设计网站网络营销公司哪家可靠
  • 微信搜一搜怎么做推广武汉好的seo优化网
  • 新建网站如何让百度收录上海推广系统
  • 个人网站可以做充值360提交入口网址
  • 福州网站制作策划百度竞价课程
  • 专业的外贸网站建设公司品牌软文
  • 新生活cms系统下载宁波seo网页怎么优化
  • wordpress 侧边栏宽度昆明优化网站公司
  • 山东滨州疫情最新消息快速排名优化公司
  • 网站建设及推广外包百度公司高管排名
  • 东莞做微网站建设价格网站排名掉了怎么恢复
  • 桂林旅游网站谷歌浏览器怎么下载
  • 安徽省建设工程资料上传网站绍兴百度推广优化排名
  • 网站没有index.htmlseo优化行业
  • 网站怎么做直播功能吗长沙哪家网络公司做网站好
  • 广州一共几个区兰州seo关键词优化
  • dw怎么做鲜花网站片多多可以免费看电视剧吗
  • 网站平台系统设计公司发外链的网址
  • 网站 备案上海有名网站建站开发公司