LeetCode -- Valid Parenthese

Question:

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.

Analysis:

问题描述:给出一个字符串,包含‘(‘‘)‘‘{‘‘}‘‘[‘ and ‘]‘确定它是否是有效地匹配括号。

思路:一看到括号匹配问题肯定想到用栈。遇到左括号就进栈;遇到右括号若栈顶元素与之匹配则POP出栈顶元素,若不匹配则返回false。

注意特殊情况:如"{}[]()", 或者“{[}]”, 或者“{{}}{{”等情况。

Answer:

public class Solution {
    public boolean isValid(String s) {
        char[] ch = s.toCharArray();
        int n = ch.length;
        if(ch.length == 0 || ch.length % 2 != 0)
            return false;
        if(ch[0] == ‘}‘ || ch[0] == ‘]‘ || ch[0] == ‘)‘)
            return false;
        Stack<Character> st = new Stack<Character>();
        st.add(ch[0]);
        int i = 1;
        while(!st.isEmpty() && i < n) {
            if(ch[i] == ‘{‘ || ch[i] == ‘[‘ || ch[i] == ‘(‘) {
                st.add(ch[i]);
                i++;
            } else {
                if(st.isEmpty())
                    return false;
                char c = st.pop();
                if(c == ‘{‘ && ch[i] != ‘}‘ || c == ‘[‘ && ch[i] != ‘]‘
                        || c == ‘(‘ && ch[i] != ‘)‘)
                    return false;
                i++;
                if(i < n && (ch[i] == ‘{‘ || ch[i] == ‘[‘ || ch[i] == ‘(‘)) {
                    st.add(ch[i]);
                    i++;
                }
            }
        }
        System.out.println(i +" " +st.size());
        if(!st.isEmpty() || i < n - 1)
            return false;

        return true;
    }

}
时间: 2024-10-12 11:08:53

LeetCode -- Valid Parenthese的相关文章

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

LeetCode: Valid Parentheses [020]

[题目] 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 "([)]

LeetCode: Valid Palindrome [125]

[题目] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider

LeetCode: Valid Sudoku [035]

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (part

LeetCode——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 "([)]"

Leetcode: Valid Parentheses 有效的括号匹配

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 "(]"

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

LeetCode: Valid Number [066]

[题目] Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambig

[LeetCode] Valid Palindrome [10]

题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider t