Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement verification parentheses in C++

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report