Share my 6 lines and 2ms c++ code


  • 1
    J
    bool isValid(string s) {
    	stack<char>st;
    	for (string::iterator i = s.begin(); i != s.end(); ++i){
    		if (!st.empty() && ((*i - st.top() == 1) || (*i - st.top() == 2)))st.pop();
    		else st.push(*i);
    	}
    	return st.empty();
    }

  • 0
    Z

    You did not push the elements in s into stack. How do you get the top element?


Log in to reply
 

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