LeetCode 20. Valid Parentheses(c++)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 20. Valid Parentheses(c++)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
利用棧的操作,遇到"(","[","{"即進(jìn)棧,遇到")","]","}"判斷是否與棧頂匹配,若不匹配則false。
class Solution { public:bool isValid(string s) {stack<char> c;for(int i=0;i<s.size();i++){if(!c.empty()){if(s[i]=='('||s[i]=='{'||s[i]=='[')c.push(s[i]);else if(s[i]==')'){if(c.top()=='(') c.pop();else return false;}else if(s[i]=='}'){if(c.top()=='{') c.pop();else return false;}else if(s[i]==']'){if(c.top()=='[') c.pop();else return false;}}else c.push(s[i]);}if(!c.empty()) return false;return true;} };?
轉(zhuǎn)載于:https://www.cnblogs.com/y1040511302/p/10786203.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的LeetCode 20. Valid Parentheses(c++)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 7.16模块及软件开发目录规范
- 下一篇: pycharm新建py文件时,自动补充文