1ms C++ solution


  • 0
    S
    bool isValid(string s)
    {
    	stack<char> stk;
    
    	for (char c : s) {
    		if (c == '(' || c == '[' || c == '{')
    			stk.push(c);
    		else {
    			if (stk.empty())
    				return false;
    
    			char top = stk.top();
    			if ((c == ')') && (top != '('))
    				return false;
    			else if ((c == ']') && (top != '['))
    				return false;
    			else if ((c == '}') && (top != '{'))
    				return false;
    
    			stk.pop();
    		}
    	}
    
    	return stk.empty();
    }

Log in to reply
 

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