20. Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

一刷
题解:
一道基本的使用stack的题目。 Time Complexity - O(n), Space Complexity - O(n)

test case:
"){"
"([])"
"(("
public class Solution {
    public boolean isValid(String s) {
        if(s == null || s.length() == 0) return true;
        Stack stack = new Stack();
        for(int i=0; i

二刷

class Solution {
    public boolean isValid(String s) {
        if((s.length()&1)!=0) return false;
        Stack stack = new Stack();
        for(int i=0; i

follow up: 如果不是括号,而是这种标记符号呢?
可以依然用上述的方法,然后在stack push('h')

你可能感兴趣的:(20. Valid Parentheses)