Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement verification parentheses in C++

Shulou Source: shulou.com Published: 2022-05-31 13:53:00 09月29日 Update

This article mainly explains "how to achieve verification parentheses in C++". The content in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn how C++ can achieve verification parentheses.

Valid Parentheses validation bracket

Given a string containing just the characters "(", ")", "{", "}", "[and"] ", determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.

Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: ()

Output: true

Example 2:

Input: "() [] {}"

Output: true

Example 3:

Input: (])

Output: false

Example 4:

Input: ([)]

Output: false

Example 5:

Input: "{[]}"

Output: true

This question allows us to verify that the input string is a parenthesis string, including curly braces, square braces, and parentheses. Here, you need to use a stack to start traversing the input string. If the current character is the left parenthesis, press it into the stack. If the right parenthesis is encountered, if the stack is empty, then return false directly. If it is not empty, take out the top element of the stack. If it is the corresponding left parenthesis, continue the loop, otherwise return false. The code is as follows:

Method 1:

Class Solution {public: bool isValid (string s) {stack parentheses; for (int I = 0; I < s.size (); + + I) {if (s [I] = = "(" | | s [I] = = "[" | | s [I] = = "{") parentheses.push (s [I]); else {if (parentheses.empty ()) return false If (s [I] = = ")" & & parentheses.top ()! = "(") return false; if (s [I] = = "]" & & parentheses.top ()! = "[") return false; if (s [I] = = "}" & & parentheses.top ()! = "{") return false; parentheses.pop () } return parentheses.empty ();}}

Method 2:

Class Solution {public: bool isValid (string s) {int n = s.size (); if (n% 2 = = 1) {return false;} unordered_map pairs = {{")", "("}, {"]", "["}, {"}", "{"}}; stack stk For (char ch: s) {if (pairs.count (ch)) {if (stk.empty () | | stk.top ()! = pairs [ch]) {return false;} stk.pop ();} else {stk.push (ch) }} return stk.empty ();}}; Thank you for your reading. The above is the content of "how to achieve verification parentheses in C++". After the study of this article, I believe you have a deeper understanding of how C++ realizes the verification brackets, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Parentheses validation clocking characters halves strings learning content methods input code elements that is ideas situations articles more knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Huawei Xiaomi MySQL