LeetCode 20 Valid Parentheses (C,C++,Java,Python)

Problem:

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.

Solution:

典型的栈应用,用栈解决,复杂度O(n)

题目大意:

给一串只包含有‘(‘‘)‘‘{‘‘}‘‘[‘ 和‘]‘,的字符串,要求判定是否是合法的。

Java源代码(用时251ms):

public class Solution {
    public boolean isValid(String s) {
        int length=s.length(),top=-1,index=0;
        char[] stack=new char[length];
        char[] str=s.toCharArray();
        while(index<length){
            if(str[index]==')'){
                if(top>=0 && stack[top]=='(')top--;
                else return false;
            }else if(str[index]=='}'){
                if(top>=0 && stack[top]=='{')top--;
                else return false;
            }else if(str[index]==']'){
                if(top>=0 && stack[top]=='[')top--;
                else return false;
            }else stack[++top]=str[index];
            index++;
        }
        return top==-1;
    }
}

C语言源代码(1ms):

bool isValid(char* s) {
    char stack[1000000];
    int top=-1;
    while(*s){
        if(*s==')'){
            if(top>=0 && stack[top]=='(')top--;
            else return false;
        }else if(*s=='}'){
            if(top>=0 && stack[top]=='{')top--;
            else return false;
        }else if(*s==']'){
            if(top>=0 && stack[top]=='[')top--;
            else return false;
        }else stack[++top]=*s;
        s++;
    }
    return top==-1;
}

C++源代码(2ms):

class Solution {
public:
    bool isValid(string s) {
        int top=-1,index=0,length=s.size();
        char* stack=(char*)malloc(sizeof(char)*length);
        while(index<length){
            if(s[index]==')'){
                if(top>=0 && stack[top]=='(')top--;
                else return false;
            }else if(s[index]=='}'){
                if(top>=0 && stack[top]=='{')top--;
                else return false;
            }else if(s[index]==']'){
                if(top>=0 && stack[top]=='[')top--;
                else return false;
            }else stack[++top]=s[index];
            index++;
        }
        return top==-1;
    }
};

Python源代码(42ms):

class Solution:
    # @param {string} s
    # @return {boolean}
    def isValid(self, s):
        length=len(s);top=-1;index=0
        stack=[' ' for i in range(length)]
        while index < length:
            if s[index]==')':
                if top>=0 and stack[top]=='(':top-=1;
                else:return False
            elif s[index]=='}':
                if top>=0 and stack[top]=='{':top-=1;
                else:return False
            elif s[index]==']':
                if top>=0 and stack[top]=='[':top-=1;
                else:return False
            else:top+=1;stack[top]=s[index]
            index+=1
        return top==-1
时间: 2024-10-18 17:11:49

LeetCode 20 Valid Parentheses (C,C++,Java,Python)的相关文章

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 020.Valid_Parentheses (Easy) 链接: 题目:https://oj.leetcode.com/problems/valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个括号字符串是否是有效的. 分析:

[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 20. Valid Parentheses 字符串

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 all valid but "(]&quo

LeetCode 32 Longest Valid Parentheses (C,C++,Java,Python)

Problem: 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 exa

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

Java [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 all valid but "(]" and "([)

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 all valid but "(]" and "([)]"

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: 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 (

LeetCode #20 Valid Parentheses (E)

[Problem] 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 "