Leetcode-valid brackets

Source: Internet
Author: User
Tags first string stack pop plantuml
Leetcode-valid brackets Table of Contents
  • 1. Easy-valid brackets
    • 1.1. Question description
    • 1.2. Example 1:
    • 1.3. Example 2:
    • 1.4. Example 3:
    • 1.5. Example 4:
    • 1.6. Example 5:
  • 2. Your answers
    • 2.1. Ideas
    • 2.2. Code
1 easy-valid brackets 1.1 Question description

Specify a string that only contains '(', ')', '{', '}', '[', ']' to determine whether the string is valid.

Valid strings must meet the following requirements:

The left brackets must be closed with the same type of right brackets.

The left brackets must be closed in the correct order.

Note that a null string can be considered a valid string.

1.2 Example 1:

Input :"()"

Output: True

1.3 Example 2:

Input: "() [] {}"

Output: True

1.4 Example 3:

Input: "(]"

Output: false

1.5 Example 4:

Input: "([)]"

Output: false

1.6 Example 5:

Input: "{[]}"

Output: True

2. Self-answer 2.1 ideas
  1. First, exclude non-conforming strings without loops, such
    • Null String
    • The string length is less than 2 (a pair of valid parentheses cannot be formed)
    • The first character is the string of the right bracket.
  2. When you use loops to determine whether a string is a valid string, use the stack to record the order of left brackets, use indexes for representation, and the indexes are symmetric (left and right variables, left [0] And right [0] are a pair of valid parentheses, and so on)
    • When the brackets are left parentheses, press the corresponding index in the left brackets into the stack.
    • When the parentheses are Parentheses, a value in stack vs is displayed. The value is a character index. pop (), the value of the stack pop-up is compared. If they are not equal, they are not valid parentheses.
  3. Note:
    • The number of right parentheses may be greater than the number of left parentheses. Therefore, before the stack pop-up value, you must determine whether the stack is empty. If it is null, false is directly returned. Invalid parentheses
    • If the left brace value is greater than the right brace value, you need to judge whether the stack is empty when Looping out. If it is null, the left brace quantity is equal to the right brace quantity, if it is not null, the number of left parentheses is greater than the number of right parentheses. False is returned. The string is invalid.
Code 2.2
Package algorithm. easy; import Java. util. stack; public class validparentheses {public static Boolean isvaild (string s) {string left = "([{"; string right = ")]}"; stack <integer> Vs = new stack <integer> (); boolean result = false; int leftpos = 0; int rightpos = 0; // empty string, return true if (S. equals ("") {return true;} // The first string is null, the string length is less than 2, or the first character is a right parenthesis, returns false if (S = NULL | S. length () <2 | (right. indexo F (S. charat (0 ))! =-1) return false; For (INT I = 0; I <S. length (); I ++) {// if the character is included in the left bracket list, push it to the stack leftpos = left. indexof (S. charat (I); If (leftpos! =-1) {. push (leftpos);} // if the character is included in the right brace list, pop it out from the stack, it is used to compare whether the index values of left and right brackets are the same. indexof (S. charat (I); If (rightpos! =-1) {// if the right brace is left and the stack is empty, the number of right brace is greater than that of left brace, and false if (vs. Empty () | rightpos! =. Pop () {return false ;}}// if the stack is empty, true is returned; otherwise, false if (. empty () {// after successful traversal, return a valid string return true;} else {return false;} public static void main (string [] ARGs) {system. out. println (isvaild ("()"); system. out. println (isvaild ("() [] {}"); system. out. println (isvaild ("(]"); system. out. println (isvaild ("([)]"); system. out. println (isvaild ("{[]}") ;}}

Date:

Author: devinkin

Created:

Validate

Leetcode-valid brackets

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.