[LeetCode]--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 a

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.

public boolean isValid(String s) {
        if (s.length() % 2 != 0)
            return false;
        char[] characters = new char[s.length()];
        int index = 0, i;
        for (i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(')
                characters[index++] = s.charAt(i);
            if (s.charAt(i) == '[')
                characters[index++] = s.charAt(i);
            if (s.charAt(i) == '{') {
                characters[index++] = s.charAt(i);
                System.out.println(index);
            }

            if (s.charAt(i) == ')') {
                if (--index < 0 || characters[index] != '(')
                    break;
            }
            if (s.charAt(i) == ']') {
                if (--index < 0 || characters[index] != '[')
                    break;
            }
            if (s.charAt(i) == '}') {
                if (--index < 0 || characters[index] != '{')
                    break;
            }
        }
        if (i == s.length() && index == 0)
            return true;
        return false;
    }

方法通过了,不过觉得有点笨,暂时也没想到好的,先就这样啦。LeedCode也没给我们提供详解。

找到一个写得比较好的程序,极力推荐大家不要看我的,看这个,又感觉被甩了几条街。

public boolean isValidParentheses(String s) {
        Stack<Character> stack = new Stack<Character>();
        for (Character c : s.toCharArray()) {
        if ("({[".contains(String.valueOf(c))) {
                stack.push(c);
            } else {
               if (!stack.isEmpty() && is_valid(stack.peek(), c)) {
                   stack.pop();
               } else {
                   return false;
               }
           }
       }
       return stack.isEmpty();
    }

    private boolean is_valid(char c1, char c2) {
        return (c1 == '(' && c2 == ')') || (c1 == '{' && c2 == '}')
            || (c1 == '[' && c2 == ']');
    }

用了系统的栈,我用的数组栈。

目录
相关文章
LeetCode 367. Valid Perfect Square
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
67 0
LeetCode 367. Valid Perfect Square
LeetCode 301. Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。 说明: 输入可能包含了除 ( 和 ) 以外的字符。
50 0
LeetCode 301. Remove Invalid Parentheses
LeetCode 242. Valid Anagram
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
54 0
LeetCode 242. Valid Anagram
LeetCode 241. Different Ways to Add Parentheses
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
55 0
LeetCode 241. Different Ways to Add Parentheses
|
canal
LeetCode 125. Valid Palindrome
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
67 0
LeetCode 125. Valid Palindrome
|
算法
LeetCode 65. Valid Number
验证给定字符串是否可以解释为十进制数。
68 0
LeetCode 65. Valid Number
Leetcode-Easy 20. Valid Parentheses
Leetcode-Easy 20. Valid Parentheses
90 0
Leetcode-Easy 20. Valid Parentheses
LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 有效字符串需满足: 左括号必须用相同类型的右括号闭合。
741 0
|
Java
[LeetCode] Valid Parentheses 验证括号是否有效闭合
链接:https://leetcode.com/problems/valid-parentheses/#/description难度:Easy题目:20.
983 0
|
人工智能 Java
LeetCode 36 Valid Sudoku(有效数独)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50118647 翻译 数独板被部分填充,空格部分用'.'来填充。
924 0