国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

LintCode 423:Valid Parentheses

2019-11-10 19:10:02
字體:
供稿:網(wǎng)友
PRoblem:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

Solution:采用stack,當(dāng)字符為上括號時push進(jìn)stack,否之則從stack pop出字符看是否匹配。(注意一些特殊情況)

class Solution {public:    /**     * @param s A string     * @return whether the string is a valid parentheses     */     //'(', ')', '{', '}', '[' and ']'    bool isValidParentheses(string& s) {        // Write your code here        int stringLength=s.length();        stack<string> check;        for(int i=0;i<stringLength;i++){            if(s.substr(i,1)=="("||s.substr(i,1)=="{"||s.substr(i,1)=="[")                check.push(s.substr(i,1));            else if(!check.empty()&&((s.substr(i,1)==")"&&check.top()!="(")||(s.substr(i,1)=="]"&&check.top()!="[")                    ||(s.substr(i,1)=="}"&&check.top()!="{")))                return 0;            else if(check.empty())                return 0;            else                check.pop();        }        if(!check.empty())            return 0;        return 1;    }};


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 溆浦县| 临西县| 宁南县| 水城县| 咸丰县| 清涧县| 曲周县| 武乡县| 休宁县| 长岭县| 大宁县| 新源县| 望谟县| 崇阳县| 万山特区| 金寨县| 郁南县| 明光市| 兴义市| 嘉义县| 松滋市| 军事| 嘉祥县| 彩票| 友谊县| 贵州省| 南木林县| 龙岩市| 周宁县| 邹平县| 蒙山县| 中方县| 高安市| 威远县| 贺州市| 德州市| 万全县| 长寿区| 凤阳县| 铜梁县| 科技|