leetcode 20

class Solution {
	public:
		bool isValid(string s) {
			stack s1;
			if (s[0] == ')' || s[0] == ']' || s[0] == '}') return false;
			size_t i = 0;
			for(; i < s.size(); ++i) {
				if(s[i] == '(' || s[i] == '{' || s[i] == '[') {
					s1.push(s[i]);
					continue;
				}
				else if(s[i] == ')' && !s1.empty() && s1.top() == '(') {
					s1.pop();
					continue;
				}
				else if(s[i] == '}' && !s1.empty() && s1.top() == '{') {
					s1.pop();
					continue;
				}
				else if(s[i] == ']' && !s1.empty() && s1.top() == '[') {
					s1.pop();
					continue;
				}
				return false;
			}
            return s1.empty();
		}
};

你可能感兴趣的:(leetcode)