leetcode[20]:Valid Parentheses

来源:互联网 发布:python程序员怎么样? 编辑:程序博客网 时间:2024/05/21 11:15

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 “([)]” are not.

bool isValid(char* s) {    int i,j,l,k;    int sk[1000];    l=strlen(s);    k=0;    for(i=0;i<l;i++)    {        switch(s[i])        {            case '(':                sk[k++]=1;                  break;            case '{':                sk[k++]=2;                break;            case '[':                sk[k++]=3;                break;                      case ')':                if(k==0 ||sk[--k]!=1) return false;                break;            case '}':                if(k==0 ||sk[--k]!=2) return false;                break;            case ']':                if(k==0 ||sk[--k]!=3) return false;                break;            default:                ;            }    }    if(k==0) return true;    else return false;}

使用数组作为线性堆栈。

0 0
原创粉丝点击