【LeetCode算法】Valid Parentheses

LeetCode第20题

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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

思路:

本来我的想法是不管(),[],{},都是在一起的,我一对一对的删掉,最后删空了,就符合要求

代码

class Solution {
    public boolean isValid(String s) {
        if(s.length()>Integer.MAX_VALUE){
            return true;
        }
        for(int i = 0;i<3;i++){
            for(int j = 0;j<s.length();j++){
                s = s.replace("()","");
                System.out.println(s);
                s = s.replace("[]","");
                System.out.println(s);
                s = s.replace("{}","");
                System.out.println(s);
            }
        }
        if("".equals(s)){
            return true;
        }else{
            return false;
        }
    }
}

结果

搞这么多符号,这不故意整我吗

百度了下,都说用栈,代码如下

class Solution {
    public boolean isValid(String s) {
        Stack<String> stack = new Stack<String>();
        for (int i = 0; i < s.length(); i++) {
            char candidate = s.charAt(i);
            if (candidate == ‘{‘ || candidate == ‘[‘ || candidate == ‘(‘) {
                stack.push(candidate + "");
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                if ((candidate == ‘}‘ && stack.peek().equals("{")) ||
                        (candidate == ‘]‘ && stack.peek().equals("[")) ||
                        (candidate == ‘)‘ && stack.peek().equals("("))) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if (stack.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}

就是利用后进先出的原理,其实跟我的思路差不多,但是性能要好很多,哈哈

原文地址:https://www.cnblogs.com/anni-qianqian/p/9061561.html

时间: 2024-10-11 21:39:42

【LeetCode算法】Valid Parentheses的相关文章

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

LeetCode: Longest Valid Parentheses [031]

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. LeetCode: Lo

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]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

Leetcode Longest Valid Parentheses 结题报告

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode] Longest Valid Parentheses 解题思路

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode][JavaScript]Valid Parentheses

https://leetcode.com/problems/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 &quo

LeetCode -- Longest Valid Parentheses(Dynamic Programming)

题目地址:https://leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring

leetCode 20.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 "(]" a