String stack operation to pay attention to detail issues

Source: Internet
Author: User

A string S consisting of N characters is considered to being properly nested if any of the following conditions is T Rue

  • S is empty;
  • S have the form "(U)" or "[u]" or "{u}" where U is a properly nested string;
  • S has the form "VW" where V and W is properly nested strings.

For example, the string "{[()]}" was properly nested but "([) ()]" was not.

Write a function:

int solution (char *s);

That, given a string S consisting of N characters, returns 1 if S is properly nested and 0 otherwise.

For example, given s = "{[() ()]}", the function should return 1 and given S = "([) ()]", the function sho Uld return 0, as explained above.

Assume that:

  • N is an integer within the range [0..200,000];
  • String S consists only of the following characters: "(", "{", "[", "]", "}< /c13> "and/or")".

Complexity:

  • Expected worst-case time complexity is O (N);
  • Expected worst-case space complexity is O (N) (not counting the storage required for input arguments).

Copyright 2009–2015 by Codility Limited. All rights Reserved. Unauthorized copying, publication or disclosure prohibited.

1. Details to be aware of:

A. After each peek, it is necessary to check for NULL, and if this step is missing and the mynode->x operation is performed directly, a segment error will occur.

B. Pay attention to the symmetry of STR, not every normal end of the loop is correct, there may be a case of less right parenthesis, it is necessary to determine if the stack is not empty, then return false;

C. Be aware that temp must be + + per time, otherwise the loop will not work.

2. Code:
1 //You can write to stdout for debugging purposes, e.g.2 //printf ("This is a debug message\n");3#include <stdlib.h>4 5typedefstructnode{6     Charx;7     structnode*Next;8 }tnode;9 Tentypedefstructstack{ OneTnode *top; ATnode *Bottom; - }tstack; -  the voidPush (tnode *n,tstack *S) - { -     if(S->top = =NULL) -     { +S->top =N; -S->bottom =N; +     } A     Else at     { -N->next = s->top; -S->top =N; -     } - } -  inTnode *peek (Tstack *S) - { to     if(S->top = =NULL) +     { -         returnNULL; the     } *     Else $     {Panax Notoginseng         returnS->top; -     } the } +  ATnode *pop (Tstack *S) the { +     if(S->top = =NULL) -     { $         returnNULL; $     } -     if(S->top = = s->bottom) -     { theTnode *temp = s->top; -S->top =NULL;WuyiS->bottom =NULL; the         returntemp; -     } Wu     Else -     { AboutTnode *temp = s->top; $S->top = s->top->Next; -         returntemp; -     } - } A  + intSolution (Char*S) { the     //Write your code in C99 -      $Tstack *mystack = malloc (sizeof(tstack)); theMystack->top =NULL; theMystack->bottom =NULL; the      the     //int len = strlen (S); -     Char*temp =S; inTnode *mynode =NULL; the     //int i; the      while(*temp) About     { the         //printf ("%c\n", *temp); the         if(*temp = ='('|| *temp = ='['|| *temp = ='{') the         { +Mynode = malloc (sizeof(tnode)); -mynode->x = *temp; the push (mynode,mystack);Bayi         } the         Else the         { -Mynode =Peek (mystack); -             if(Mynode = =NULL) the             { the                 return 0; the             } the             if(*temp = =')') -             { the                 if(Mynode->x = ='(') the                 { theMynode =pop (mystack);94                 } the                 Else the                 { the                     return 0;98                 } About             } -             if(*temp = =']')101             {102                 if(Mynode->x = ='[')103                 {104Mynode =pop (mystack); the                 }106                 Else107                 {108                     return 0;109                 } the             }111             if(*temp = ='}') the             {113                 if(Mynode->x = ='{') the                 { theMynode =pop (mystack); the                 }117                 Else118                 {119                     return 0; -                 }121             }122         }123         124temp++; the     }126Mynode =Peek (mystack);127     if(Mynode = =NULL) -     {129         return 1; the     }131     Else the     {133         return 0;134     }135     136}

String stack operation to pay attention to detail issues

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.