RATSENO

[Leetcode]Valid Parentheses 본문

DEV/코딩테스트 문제풀기

[Leetcode]Valid Parentheses

RATSENO 2020. 3. 16. 21:09
import java.util.Stack;

public class ValidParentheses {
    public static 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();
    }

    public static void main(String[] args) {
        //isValid("()");
        isValid("(){}[]");
    }
}

'DEV > 코딩테스트 문제풀기' 카테고리의 다른 글

[Leetcode P67]2진수 더하기  (0) 2021.03.22
[JAVA]프로그래머스(level1) - 체육복  (0) 2020.03.18
[Leetcode]Longest Common Prefix  (0) 2020.03.16
[Leetcode]Roman to Integer  (0) 2020.03.16
[Leetcode]Palindrome Number  (0) 2020.03.15
Comments