Valid Parentheses

来源:互联网 发布:手机qq透明主题软件 编辑:程序博客网 时间:2024/06/05 13:22
class Solution {public:    bool isValid(string s)     {        if(s.empty()||s.size()==1)            return false;        stack<char> stackdata;        int i=0;        const int n=s.size();        while(i<n)        {            while(s[i]=='(' || s[i]=='{' || s[i]=='[' && i<n)            {                stackdata.push(s[i]);                ++i;            }            while(s[i]==')' || s[i]=='}' || s[i]==']' && i<n)            {                if(!stackdata.empty())                {                    char temp=stackdata.top();                    stackdata.pop();                    if(temp=='('&& s[i]!=')' || temp=='['&& s[i]!=']'|| temp=='{'&& s[i]!='}')    return false;    if(temp=='('&& s[i]==')' || temp=='['&& s[i]==']'|| temp=='{'&& s[i]=='}')    ++i;                                    }                else                    return false;            }        }                if(!stackdata.empty())            return false;        else            return true;            }};

0 0
原创粉丝点击