Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use leetcode20. Valid parentheses

Shulou Source: shulou.com Published: 2022-06-02 19:35:03 10月03日 Update

This article mainly introduces "how to use leetcode20. Valid parentheses". In daily operation, I believe many people are using leetcode20. There are doubts on the issue of valid parentheses. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation, hoping to help you answer the doubts about "how to use leetcode20. Valid parentheses"! Next, please follow the editor to study!

1. Concept introduction

Stack, also known as stack, is an important data structure. From the point of view of data structure, stack is also a linear table, and its particularity lies in that the basic operation of stack is a subset of linear table operation, and it is a linear table with limited operation, so it can be called limited data structure. Restrict it to insert or delete only at the end of the table. The end of the table is called the top of the stack, and accordingly, the header is called the bottom of the stack. The basic operation of the stack not only inserts and deletes at the top of the stack, but also initializes the stack, determines the emptiness and takes the top elements of the stack and so on.

two。 Topic description

Given a string that includes only'(',')','{','}','[',']', determine whether the string is valid.

A valid string must satisfy:

The left parenthesis must be closed with the same type of closing parenthesis.

The left parenthesis must be closed in the correct order.

Note that an empty string can be considered a valid string.

Example 1:

Enter: "()"

Output: true

Example 2:

Enter: "() [] {}"

Output: true

Example 3:

Enter: "(]"

Output: false

Example 4:

Enter: "([)]"

Output: false

Example 5:

Enter: "{[]}"

Output: true

Source: power buckle (LeetCode)

Link: https://leetcode-cn.com/problems/valid-parentheses

3. Solution 1, stack 1, traversal string 2, press stack 3 when left symbol, compare stack with current character when right match, return false4 if not equal, end traversal time, return false if stack is not empty, otherwise return true*/4. Test result

Solution 1. Stack

5. Stack / * title: leetcode20. Valid parentheses author: xidoublestarmethod: stack type: Cdate: 2020-5-29*/bool isValid (char* s) {if (! strlen (s)) return true; int s_len = strlen (s); char* mid = (char*) malloc (s_len); int top = 0; for (int I = 0; I < s_len) ) {if (s [I] = ='('| | s [I] = ='['| | s [I] = ='{') mid [top++] = s [I]; else if (s [I] = =')'& (! top | | mid [--top]! ='(') return false Else if (s [I] = =']'& (! top) | | mid [--top]! ='[') return false; else if (s [I] = ='}'& & (! top | | mid [- top]! ='{') return false;} free (mid); return top? False: true;} at this point, the study on "how to use leetcode20. Valid parentheses" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Valid parentheses characters strings examples input output learning data data structures linearity structure basic operations ideas more solutions help closure practical special Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Microsoft OPPO Reno Shulou Technology