#20 Valid Parentheses

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.