Title Link: https://leetcode.com/problems/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.
typedef struct STACKRECORD { //Do not consider overflow stack implementation simple stack char ch[100];int top;} *stack;bool isEmpty (Stack stack) {if (stack->top >= 0) return False;return true;} Stack init () {Stack stack = (stack) malloc (sizeof (struct stackrecord)); stack->top = -1;return stack;} void push (char ch, stack stack) {stack->ch[++stack->top] = ch;} Char top (Stack stack) { //returns the top element of the stack, returning '!isempty ' if (stack) return stack->ch[stack->top]; when empty stack return 0;} void pop (stack stack) {--stack->top;} BOOL IsValid (char* s) {Stack stack = init (); while (*s) {switch (*s) {case ' (': ///left parenthesis into stack case ' [': Case ' {':p ush (*s, Stack Break;case ') ': //Right parenthesis to see if the top of the stack is a corresponding opening parenthesis. Does not return false, otherwise out of stack if (Top (stack)! = ' (') return False;pop (stack); Break;case ': if (stack) = ' [') Return False;pop (stack ); Break;case '} ': if (top (stack) = ' {') return False;pop (stack); Break;default:return false;} ++s;} if (!isempty (stack)) //stack is not empty description has extra opening parenthesis return False;return true;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
#20 Valid Parentheses