LeetCode algorithm stack—valid parentheses

Table of contents

valid parentheses

Data structures used:

Bit operations, Maps and Stacks

Stack commonly used functions:

answer:

Code:

operation result;


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

A valid string must satisfy:

  1. The opening bracket must be closed by a closing bracket of the same type.
  2. Opening brackets must be closed in the correct order.
  3. Every right bracket has a corresponding left bracket of the same type.

Example 1:

输入:s = "()"
输出:true

Example 2:

输入:s = "()[]{}"
输出:true

Example 3:

Import:s = "(]"
Output:false

hint:

  • 1 <= s.length <= 104
  • s 仅ゆく訳 '()[]{}' 组组

Data structures used:

Bit operations, Maps and Stacks
  1. Use bit operations (n&1)==1 to determine whether the string length n is an odd number. If it is an odd number, it will return false directly. Because valid bracket sequences must appear in pairs.

  2. Use Map data structure buckets to store the mapping relationship between left and right brackets. Key-value pairs are in the form of left bracket-right bracket.

  3. The Stack stack data structure is used stack to store the left bracket during the traversal process

Stack commonly used functions:
  • push(E item): Push the element item onto the top of the stack.

  • pop(): Pop and return the top element of the stack.

  • peek(): Returns the element at the top of the stack without popping it.

  • isEmpty(): Determine whether the stack is empty, return true if it is empty, otherwise return false.

  • size(): Returns the number of elements in the stack.

answer:

  1. Traverse each character of the string. If the current character is a left bracket, push it onto the stack; if the current character is a right bracket, determine whether the stack is empty or whether the current right bracket matches the left bracket on the top of the stack. If there is no match, false is returned; if there is a match, the left parenthesis at the top of the stack is popped.

  2. Finally, it is judged whether the stack is empty. If it is empty, it means that all left brackets have corresponding right brackets to match, and true is returned; otherwise, false is returned.

Code:

class Solution {
    public boolean isValid(String s) {
      int n=s.length();
      // 判断奇偶
      if((n&1)==1) return false;
      // 存储左右括号映射
      Map<Character,Character> buckets=new HashMap<>();
      buckets.put('(',')');
      buckets.put('[',']');
      buckets.put('{','}');
      // 存储左括号
      Stack<Character> stack =new Stack<>();
      // 存储当前括号字符
      char bucket;
      // 遍历处理
      for(int i=0;i<n;i++){
        bucket=s.charAt(i);
        if(buckets.containsKey(bucket)){
          stack.push(bucket);// 为左括号,入栈
        }else if(stack.isEmpty() || bucket !=buckets.get(stack.peek())){
          return false;//当前为右括号但是此时栈为空或和栈顶不匹配
        }else{
          stack.pop();//当前右括号和栈顶匹配,弹出栈顶
        }
      }
      return stack.isEmpty();
    }
}

operation result;

Guess you like

Origin blog.csdn.net/qq_62799214/article/details/133913242