0ms C Solution


  • 0
    J
    bool isValid(char* s) {
        char *stack = (char*)malloc(sizeof(int) * strlen(s) + 1);
        int tail = 1;
        stack[0] = 0;
        for(int i = 0; s[i]; i++){
            switch(s[i]){
                case '(':
                case '[':
                case '{': stack[tail++] = s[i]; break;
                case ')': if (stack[--tail] != '(') return false; break;
                case ']': if (stack[--tail] != '[') return false; break;
                case '}': if (stack[--tail] != '{') return false; break;
            }
        }
        free(stack);
        return tail == 1;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.