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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

LintCode 423:Valid Parentheses

2019-11-10 19:20:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
PRoblem:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

Solution:采用stack,當(dāng)字符為上括號(hào)時(shí)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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 望谟县| 陆川县| 洛宁县| 泸州市| 定边县| 葫芦岛市| 门头沟区| 巴里| 陆河县| 周至县| 班玛县| 靖远县| 云龙县| 桐柏县| 台东市| 巨鹿县| 精河县| 松原市| 洪江市| 都匀市| 开封市| 屏山县| 五峰| 葵青区| 东辽县| 北川| 罗平县| 云龙县| 姜堰市| 山阳县| 高青县| 濮阳县| 松江区| 阿拉尔市| 资阳市| 时尚| 比如县| 瓮安县| 邵东县| 三明市| 龙海市|