Algorithm practice notes - common methods of stacks and algorithm exercises

Introduction to common methods

     Stack<Character> characters = new Stack<>();
        //判断栈是否为空
        boolean empty = characters.empty();
        //将a压入栈底,同时也返回a
        Character push = characters.push('a');
        //返回栈顶的元素但是并不会删除它
        Character peek = characters.peek();
        //返回栈顶的元素,并将它删除
        Character pop = characters.pop();
        //在堆栈中搜索a,如果发现了,则返回它相对于栈顶
        //的偏移量。否则,返回-1。
        int search = characters.search('a');
        boolean empty1 = characters.isEmpty();
       

Buckle Exercises

Leetcode 20. Valid Parentheses

insert image description here
Solution one

class Solution {
    
    
    Stack<Character> characters = new Stack<>();
    public boolean isValid(String s) {
    
    
       char[] chars = s.toCharArray();
       for(int i=0;i<s.length();i++)
       {
    
    
        if(chars[i]=='(') characters.push(')');
        else if(chars[i]=='{') characters.push('}');
        else if(chars[i]=='[') characters.push(']');
        else if(characters.isEmpty()||chars[i]!=characters.pop()) return false;
       }
       return characters.isEmpty();                                                                         

    }
}

Idea:
Take advantage of the last-in-first-out feature of the stack
and use the left half to judge. If it corresponds to it, the corresponding one will be pushed into the stack, and next time if it is not on the left, the one in the stack will be compared with it and popped out of the stack.

Leetcode 32. Longest Valid Brackets

insert image description here

class Solution {
    
    
    public int longestValidParentheses(String s) {
    
    
        int maxans=0;
        Stack<Integer> stack = new Stack<>();
        stack.push(-1);
        if(s.length()==0||s.length()==1){
    
    
            return 0;
        }
        char[] chars = s.toCharArray();
        for(int i=0;i<s.length();i++){
    
    
            if(chars[i]=='(') stack.push(i);
            else{
    
    
                stack.pop();
                if(stack.isEmpty()){
    
    
                     stack.push(i);
                }
                else{
    
    
                     maxans = Math.max(maxans, i-stack.peek());
                }
            }
        }
       return maxans;

    }
}

Analysis of ideas:
First of all, my first reaction is to use the stack. When encountering (, push it into the stack, and when encountering), pop the top element of the stack, so that the matching of () can be achieved, but what is needed is to find out the
record Length and finding the longest string
We first define a maxans that is the longest string
Next we need to find the longest string
We can first convert the string into an array and use its element subscript to find the longest string :
The idea is that we can put the element subscript onto the stack, and record the last unmatched element subscript, so that the difference can be calculated twice, and each time we use a mathematical function to find the largest data record. Since the first
time No element we can put -1 on the stack first

Guess you like

Origin blog.csdn.net/qq_54796785/article/details/128741035