Leetcode---Valid parentheses

Source: Internet
Author: User

Title:

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.

Main topic:given a string, determine whether the parentheses in the string are paired. Ideas:The question of whether the parentheses are paired is one of the most important applications of the stack in the data structure, and it is quite easy to do so with the data structure only after the foundation. The general idea is that when the left parenthesis is encountered in the stack, when the right half of the parentheses when the top of the stack to see if it matches, if the match pops up the top of the stack to continue to scan the string, if not match the direct return false. When the string scan ends, the elements in the stack are exactly empty when the match succeeds and returns true. Code:
class        Solution {Public:bool IsValid (std::string s) {const int size = S.size ();                Std::stack<char> v;        if (size% 2) {return false;        } else if (size = = 0) {return true;            } for (int i = 0; i < size; ++i) {const char TMP = s[i];            if (tmp = = ' (' | | tmp = = ' {' | | tmp = = ' [') {V.push (TMP); } else if ((!v.empty ()) && ((tmp = = ') ' && v.top () = = ' (') | | (TMP = = '] ' && v.top () = = ' [') | |            (TMP = = '} ' && v.top () = = ' {'))) {V.pop ();            } else {return false;    }} return V.empty (); }};
The difference in ASCII code can also be used when judging, because each ASCII code value to match is: (--->40,)--->41, [--->91,]--->93, {--->123,}---- >125 so you can determine whether the difference in ASCII is equal to 1 or both to get the answer, this way of thinking is seen in the answer area of Leetcode, when his code is written like this:
BOOL IsValid (string s) {                stack<char> pare;                for (auto c:s) {            if (c== ' | | | c== ' {' | | c== ' [') {                pare.push (c);            } else{                if (Pare.empty ()) return false;                                if (C-pare.top () >2) return false;                Becasue in Ascii, (: +,): A, [:]:93, {: 123,}:125                Pare.pop ();}        }        return Pare.empty ();}

But in my opinion this code is a problem. In this code, he directly judge all the circumstances of the scan value minus the stack top element is less than the number of matches, but in fact 92 ASCII code corresponding to the character is \, so when the [\] such a string of characters, there is a problem, the following is the result of my test in Leetcode:
Visible program is indeed a bug, reported the runtime error errors. The error of the runtime here is not known whether this code is reported or the internal standard answer to the report I do not know. But it did ac the problem when the code was submitted. It can be seen that Leetcode is not comprehensive on the test case of this problem. Of course, this method of judgment is also possible, but to determine whether the difference between the classification is 1 or 2.

Leetcode---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.