20. Valid Parentheses
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供芷江網(wǎng)站建設、芷江做網(wǎng)站、芷江網(wǎng)站設計、芷江網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、芷江企業(yè)網(wǎng)站模板建站服務,10年芷江做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
題目大意:
括號匹配問題。
思路:
采用壓棧出棧來處理。
class Solution { public: bool isValid(string s) { if(s.size() == 0 || s.size() % 2 == 1) return false; mapbrackets; brackets.insert(pair ('}', '{')); brackets.insert(pair (']', '[')); brackets.insert(pair (')', '(')); stack mystack; for(int i = 0 ;i < s.size();i++) { if(s[i] == '{' || s[i] == '[' || s[i] == '(' ) { mystack.push(s[i]); } else { if( mystack.size() < 1) return false; if(brackets[s[i]] == mystack.top()) mystack.pop(); else return false; } } if(mystack.size() == 0) return true; else return false; } };
2016-08-11 00:30:15