码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-10 17:21:30      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:c   c++   leetcode   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


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

标签:c   c++   leetcode   java   python   

原文地址:http://blog.csdn.net/runningtortoises/article/details/45621933

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!