One map , one stack , is it complicate ?


  • 0
    L
    bool isValid(string s) {
        if(s.size()==0)
        {
            return false;
        }
        map<char , char> mymap;
        mymap['(']='0';mymap['[']='0';mymap['{']='0';
        mymap[')']='(';mymap[']']='[';mymap['}']='{';
        vector<char> stack;
        for(int i=0;i<s.size();i++)
        {
            if(mymap[s[i]]=='0')
            {
                stack.push_back(s[i]);
            }
            else
            {
                if(stack.size()==0)
                {
                    return false;
                }
                char tmp=stack.back();
                if(tmp==mymap[s[i]])
                {
                    stack.pop_back();
                }
                else
                {
                    return false;
                }
            }
        }
        
        return stack.size()==0 ? true:false;
    
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.