0ms Solution? Kidding me? Although the code is nice!


  • 0

    I dont know the code is running 0ms? The test case is too small?

    class Solution {
    public:
    	bool isValid(string s){
    		//suppose null string is illegal
    		if(s.size( ) == 0){
    			return false;
    		}
    		stack<char> st;
    		map<char, char> cm{ {')','(' },{']','[' },{'}','{' } };
    		for(int i = 0; i != s.size( ); ++i){
    			switch(s[i]){
    			case '(':
    			case '[':
    			case '{':
    				st.push(s[i]);
    				break;
    			case ')':
    			case ']':
    			case '}':
    				if(st.empty( ) || st.top( ) != cm[s[i]]){
    					return false;
    				}
    				else{
    					st.pop( );
    					break;
    				}
    			//if illegal character appears, like @ # $ % a ... , return false
    			default:
    				return false;
    			}
    		}
    		return st.empty( );
    	}
    };
    

Log in to reply
 

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