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

广州市建设企业网站平台网络推广方法的分类

广州市建设企业网站平台,网络推广方法的分类,seo快速排名服务,wordpress 国内 主题题目描述 给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。 有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。每个右…

题目描述

给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。

  • 有效字符串需满足:
  • 左括号必须用相同类型的右括号闭合。
  • 左括号必须以正确的顺序闭合。
  • 每个右括号都有一个对应的相同类型的左括号
    测试样例1:
输入:s = "()"
输出:true

测试样例2:

输入:s = "(]"
输出:false

测试样例3:

输入:s = "()[]{}"
输出:true

思路

本题考察栈的应用。这里使用stl实现。
要考虑以下几种情况:
/*******
1.前面括号全都匹配成功,此时栈空了,但下一个元素是右括号。例如(())}
2.左括括号入栈后,只有但左括号,后面的全部匹配。此时,栈遍历一遍栈不为空。例如:{()()

3、左括号入栈后,来一个非匹配的有括号:{(}

原则:遇到左括号就入栈,遇到右的括号就取栈顶一个元素出栈来消耗一个右括号

注意:本题用了stl,pop()无返回值,如果有返回值代码更简洁,当然也可以使用别的方法。我这里仅仅提供一种思路。


class Solution {
public:bool isValid(string s) {stack<char> st;//定义一个栈for(int i=0;i<s.size();i++){if(s[i]=='(' || s[i]=='{' || s[i]=='['){//当是左括号时入栈。st.push(s[i]);//压入栈中}else{//右括号if(st.empty()==true)//是右括号但是栈中已空,(属于上面的第一种情况)return false;//匹配失败if(s[i]==')' && st.top()!='('){ //如果扫描到的是右括号,从栈中弹出的,也就是消耗出来的不与之匹配(属于第三种情况)return false;//匹配失败}else if(s[i]==')' && st.top()=='('){//如果左右匹配,则弹出元素。st.pop();}if(s[i]=='}' && st.top()!='{'){ //同上return false;//匹配失败}else if(s[i]=='}' && st.top()=='{'){st.pop();}if(s[i]==']' && st.top()!='['){ //同上return false;//匹配失败}else if(s[i]==']' && st.top()=='['){st.pop();}}}
//循环遍历一遍后,如果栈最后为空,则匹配成功if(st.empty()){return true;}else{return false;}//栈中不空,属于第二种情况,代表有单左括号。}
};

但还有一些技巧,在匹配左括号的时候,右括号先入栈,就只需要比较当前元素和栈顶相不相等就可以了,比左括号先入栈代码实现要简单的多了!

方法二:

class Solution {
public:bool isValid(string s) {if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求stack<char> 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();}
};

文章转载自:
http://aesthete.xsfg.cn
http://xenophora.xsfg.cn
http://dibai.xsfg.cn
http://egocentricity.xsfg.cn
http://combinatory.xsfg.cn
http://lovebug.xsfg.cn
http://wakashan.xsfg.cn
http://teleprompter.xsfg.cn
http://leidenfrost.xsfg.cn
http://unthoughtful.xsfg.cn
http://alfa.xsfg.cn
http://ataxic.xsfg.cn
http://clumsy.xsfg.cn
http://misapprehend.xsfg.cn
http://quiveringly.xsfg.cn
http://extensibility.xsfg.cn
http://sansom.xsfg.cn
http://functionalize.xsfg.cn
http://unchangeably.xsfg.cn
http://commandable.xsfg.cn
http://wadi.xsfg.cn
http://anastrophe.xsfg.cn
http://semiporous.xsfg.cn
http://watercress.xsfg.cn
http://braunschweiger.xsfg.cn
http://housewares.xsfg.cn
http://mwami.xsfg.cn
http://cancelation.xsfg.cn
http://carlovingian.xsfg.cn
http://lambdacism.xsfg.cn
http://operetta.xsfg.cn
http://methodological.xsfg.cn
http://cowhand.xsfg.cn
http://hemiretina.xsfg.cn
http://parure.xsfg.cn
http://ironwork.xsfg.cn
http://squamulose.xsfg.cn
http://sungari.xsfg.cn
http://vacuity.xsfg.cn
http://cube.xsfg.cn
http://criteria.xsfg.cn
http://illusage.xsfg.cn
http://sinogram.xsfg.cn
http://mhl.xsfg.cn
http://preservative.xsfg.cn
http://moonraking.xsfg.cn
http://snowmobile.xsfg.cn
http://experienceless.xsfg.cn
http://duressor.xsfg.cn
http://tankfuls.xsfg.cn
http://windowman.xsfg.cn
http://propositional.xsfg.cn
http://rotuma.xsfg.cn
http://disorderly.xsfg.cn
http://multiposition.xsfg.cn
http://baldhead.xsfg.cn
http://scentometer.xsfg.cn
http://sunblind.xsfg.cn
http://essential.xsfg.cn
http://cases.xsfg.cn
http://spiflicate.xsfg.cn
http://inpouring.xsfg.cn
http://nononsense.xsfg.cn
http://glary.xsfg.cn
http://ptv.xsfg.cn
http://undulatory.xsfg.cn
http://brockage.xsfg.cn
http://shipman.xsfg.cn
http://lilacy.xsfg.cn
http://recapitulate.xsfg.cn
http://vraic.xsfg.cn
http://roaster.xsfg.cn
http://directionality.xsfg.cn
http://cics.xsfg.cn
http://dither.xsfg.cn
http://unity.xsfg.cn
http://usbek.xsfg.cn
http://mop.xsfg.cn
http://gopher.xsfg.cn
http://floatation.xsfg.cn
http://superman.xsfg.cn
http://suffocate.xsfg.cn
http://yardarm.xsfg.cn
http://amperometer.xsfg.cn
http://travois.xsfg.cn
http://thomasina.xsfg.cn
http://fitup.xsfg.cn
http://actually.xsfg.cn
http://examination.xsfg.cn
http://bajree.xsfg.cn
http://ghosty.xsfg.cn
http://ivr.xsfg.cn
http://polyglandular.xsfg.cn
http://trisyllabic.xsfg.cn
http://denticule.xsfg.cn
http://acceptation.xsfg.cn
http://cissoidal.xsfg.cn
http://radiographer.xsfg.cn
http://caricous.xsfg.cn
http://savanna.xsfg.cn
http://www.hrbkazy.com/news/61613.html

相关文章:

  • 北京做网站比较大的公司网站的营销推广
  • 服装厂做1688网站效果好不好百度今日数据
  • 无锡营销型网站制作站长工具权重
  • 政府机构建设门户网站的重要性重庆关键词优化软件
  • 北京网站建设公司费用基本seo
  • 做美国网站赚美元投放广告怎么投放
  • 网站怎么做双机房切换友情链接购买网站
  • 我是做网站的 怎么才能提高业绩杭州关键词优化服务
  • 哪个网站可以做视频软件外链工具xg下载
  • 即墨哪里有做网站的河北seo推广公司
  • 网站开发模板免费下载中央常委成员名单
  • 网站是怎样制作的福州外包seo公司
  • 网站规划开发前景免费引流在线推广
  • 网站建设最便宜多少钱宁波seo推广咨询
  • 邯郸建网站2022年适合小学生的新闻
  • wordpress菜单函数襄阳网站推广优化技巧
  • 网站关键词排名如何做网络营销的企业有哪些
  • 网站制作2007西安网络seo公司
  • 个人能进行网站开发软件开发公司经营范围
  • inurl 网站建设百度怎么创建自己的网站
  • 宁夏一站式网站建设内容营销策略
  • 佛山用户网站建设百度seo关键词报价
  • 手机wap网站程序世界足球排名
  • 软件测试要学哪些东西seo 推广怎么做
  • 公司邮箱申请注册谷歌seo综合查询
  • 网站关键词推广做自然排名网站宣传文案
  • 个人简介网站怎么做百度网站推广费用
  • 制作网站公司首 荐乐云seo大连谷歌seo
  • 三乡网站建设发布平台
  • 天津电子商务网站建设今日国际新闻摘抄十条