Leetcode - Valid Parentheses

Leetcode - Valid Parentheses_第1张图片
Paste_Image.png

My code:

import java.util.Stack;

public class Solution {
    public boolean isValid(String s) {
        if (s == null || s.length() == 0)
            return false;
        Stack paren = new Stack();
        for (int i = 0; i < s.length(); i++) {
            char temp = s.charAt(i);
            if (temp == '{' || temp == '(' || temp == '[')
                paren.push(temp);
            else {
                if (paren.isEmpty())
                    return false;
                else {
                    char popStack = paren.pop();
                    if ((temp == '}' && popStack == '{') || (temp == ')' && popStack == '(')
                        || (temp == ']' && popStack == '['))
                        continue;
                    else
                        return false;
                }
            }
        }
        if (!paren.isEmpty())
            return false;
        else
            return true;
    }
}

My testing result:

Leetcode - Valid Parentheses_第2张图片

这次作业比较简单。就是利用一个栈,把左边符放入栈中,遇到右边符时,就从stack中弹出一个看看是否匹配。如果栈空或者弹出的不匹配,那就错了,否则继续。最后判断下栈是否为空,不为空也是不匹配。

**
总结:没啥好总结的。比较简单。
**
Anyway, Good luck, Richardo!

My code:

public class Solution {
    public boolean isValid(String s) {
        if (s == null || s.length() == 0)
            return false;
        Stack st = new Stack();
        st.push(s.charAt(0));
        for (int i = 1; i < s.length(); i++) {
            char curr = s.charAt(i);
            if (st.isEmpty()) {
                st.push(curr);
                continue;
            }
            char peek = st.peek();
            if ((peek == '(' && curr == ')')
                || (peek == '[' && curr == ']')
                || (peek == '{' && curr == '}')){
                st.pop();
            }
            else if (curr == ')' || curr == ']' || curr == '}') {
                return false;                
            }
            else {
                st.push(curr);
            }
        }
        if (st.isEmpty())
            return true;
        else 
            return false;
    }
}

没什么好总结的。比较简单。
拿到了zappos的一个网上测试通知。第一个测试。有点紧张,有点兴奋。

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public boolean isValid(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }
        
        Stack st = new Stack();
        for (int i = 0; i < s.length(); i++) {
            char curr = s.charAt(i);
            if (curr == '(' || curr == '{' || curr == '[') {
                st.push(curr);
            }
            else {
                if (st.isEmpty()) {
                    return false;
                }
                else if ((curr == ')' && st.peek() == '(')
                        || (curr == '}' && st.peek() == '{')
                        || (curr == ']' && st.peek() == '[')) {
                            st.pop();
                        }
                else {
                    return false;
                }
            }
        }
        
        if (st.isEmpty()) {
            return true;
        }
        else {
            return false;
        }
    }
}

简单题。一开始以为是, ()的组合,后来发现有多种括号。只能拿zhan

你可能感兴趣的:(Leetcode - Valid Parentheses)