首页 > 代码库 > 20. Valid Parentheses

20. Valid Parentheses

 

https://leetcode.com/problems/valid-parentheses/#/solutions

 

public boolean isValid(String s) {
	Stack<Character> stack = new Stack<Character>();
	for (char c : s.toCharArray()) {
		if (c == ‘(‘)
			stack.push(‘)‘);
		else if (c == ‘{‘)
			stack.push(‘}‘);
		else if (c == ‘[‘)
			stack.push(‘]‘);
		else if (stack.isEmpty() || stack.pop() != c)
			return false;
	}
	return stack.isEmpty();
}

  

20. Valid Parentheses