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) {
    char stack[10000];
    char* p= s;
    int i= 0;
    while(*p!='\0')
    {
        if(*p=='('||*p=='['||*p=='{')
        {
            i++;
            stack[i]=*p;
            p++;
        }
        else if(*p==')'&&stack[i]=='(')
        {
                i--;
                p++;
        }
        else if(*p==']'&&stack[i]=='[')
        {
                i--;
                p++;
        }
        else if(*p=='}'&&stack[i]=='{')
        {
                i--;
                p++;
        }
        else
            return 0;
    }
    if(i>0)
        return 0;
    else
        return 1;
}