LeetCode stack and queue application - 150. Reverse Polish expression evaluation

What classic problems can stacks and queues solve? Every question can be found on my homepage, welcome everyone to pay attention~~

(1) Bracket matching problem (stack)

(2) String deduplication problem (stack)

  (3) Reverse Polish expression problem (stack)

  (4) The next larger element (monotonic stack)

(5) Receive rainwater (monotone stack)

(6) Sliding window maximum problem (monotonic queue) 

(7) Top K elements with the most occurrences (priority queue)

(8) Stack implementation queue

(9) Queue implementation stack 

1. Solution 

150. Reverse Polish expression evaluation

Evaluates the expression in  reverse Polish notation .

Valid operators include  +, -, *, / . Each operand can be an integer or another reverse Polish expression.

Note  that division between two integers keeps only the integer part.

It is guaranteed that a given reverse Polish expression will always be valid. In other words, the expression will always result in a valid value and there will be no division by zero.

 

class Solution {
    //可以看官方题解视频
    public int evalRPN(String[] tokens) {
        Deque<Integer> deque=new LinkedList<>();
        for(int i=0;i<tokens.length;i++){
            String token=tokens[i];
            if(token.equals("+")){
            int nums1=deque.pop();
            int nums2=deque.pop();
            deque.push(nums1+nums2);
            }else if(token.equals("-")){
            int nums1=deque.pop();
            int nums2=deque.pop();
            deque.push(nums2-nums1);
            }else if(token.equals("*")){
            int nums1=deque.pop();
            int nums2=deque.pop();
            deque.push(nums1*nums2);
            }else if(token.equals("/")){
            int nums1=deque.pop();
            int nums2=deque.pop();
            deque.push(nums2/nums1);
            }else{
            deque.push(Integer.valueOf(token));
            }
        }
        return deque.peek();
    }
}

2. Basic knowledge of the stack

The most notable feature of the stack is: first in last out

1. Common implementation classes of stacks in Java:

The most basic implementation: Stack<Integer> stack=new Stack<>();

Double-ended queue implementation: Deque<Integer> stack=new LinkedList<>();

2. Common methods of stack

push(x) -- push an element to the tail of the queue.
pop() -- removes an element from the head of the queue.
peek() -- returns the element at the head of the queue.
empty() -- returns whether the queue is empty.

3. Basic knowledge of queues

The most notable feature of the queue is: first in first out

1. Common implementation classes of queues in Java:

Ordinary queue: Queue<Integer> queue=new LinkedList<>();

Double-ended queue: Deque<Integer> queue=new LinkedList<>();

Priority queue: PriorityQueue<Integer> queue=new PriorityQueue<>();

2. Common methods of queues

add Add an element If the queue is full, throw a IIIegaISlabEepeplian exception
remove Remove and return the element at the head of the queue If the queue is empty, throw a NoSuchElementException
element Return the element at the head of the queue If the queue is empty, then Throws a NoSuchElementException

size The number of elements
offer Add an element and return true If the queue is full, return false
poll Remove and return the element at the head of the queue If the queue is empty, return null
peek Return the element at the head of the queue If the queue is empty, return null


put adds an element, blocks if the queue is full
take removes and returns the element at the head of the queue, blocks if the queue is empty

Guess you like

Origin blog.csdn.net/w20001118/article/details/127149042