20. Valid Parentheses

来源:互联网 发布:手机数据恢复软件安卓 编辑:程序博客网 时间:2024/06/06 14:11

题目:https://leetcode.com/problems/valid-parentheses/

代码:

public class Solution {    public boolean isValid(String s) {        if(s.length()%2!=0)            return false;        int[] flag = new int[s.length()];        for(int i=0;i<s.length();i ++)        {            if(flag[i]==1)                continue;            if(s.charAt(i)=='(')            {                flag[i] = 1;                int temp = i+1;                int count = 0;                while(temp<s.length())                {                    if(flag[temp]==1)                        continue;                    if(s.charAt(temp)==')')                    {                        flag[temp] = 1;                        count = 1;                        break;                    }                    temp += 2;                }                if(count==0)                    return false;            }            else if(s.charAt(i)=='[')            {                flag[i] = 1;                int temp = i+1;                int count = 0;                while(temp<s.length())                {                    if(flag[temp]==1)                        continue;                    if(s.charAt(temp)==']')                    {                        flag[temp] = 1;                        count = 1;                        break;                    }                    temp += 2;                }                if(count == 0)                    return false;            }            else if(s.charAt(i)=='{')            {                flag[i] = 1;                int temp = i+1;                int count = 0;                while(temp<s.length())                {                    if(flag[temp]==1)                        continue;                    if(s.charAt(temp)=='}')                    {                        flag[temp] = 1;                        count = 1;                        break;                    }                    temp += 2;                }                if(count == 0)                    return false;            }        }        for(int i=0;i<s.length();i++)        {            if(flag[i]==0)                return false;        }        return true;    }   }bad idea!!time exceed=================================use stack to solvepublic class Solution {    public boolean isValid(String s) {        char[] list = new char[s.length()];        int head = 0;        for(char c : s.toCharArray())        {            switch(c)            {                case '(':                case '[':                case '{':                list[head++] = c;break;                case ')':                    if(head==0||list[--head]!='(')                        return false;                    break;                case ']':                    if(head==0||list[--head]!='[')                        return false;                    break;                case '}':                    if(head==0||list[--head]!='{')                        return false;                    break;            }        }        return head==0;    }}0ms
0 0
原创粉丝点击