LeetCode Stack and Queue Application - 1047. Remove all adjacent duplicates in a string

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 

1047. Remove All Adjacent Duplicates in a String

Given a string of lowercase letters  S, the duplicate removal operation selects two adjacent identical letters and removes them.

Repeat the deduplication operation on S until no further deduplication is possible.

Returns the final string after all deduplication operations have completed. The answer is guaranteed to be unique.

 

class Solution {
    public String removeDuplicates(String s) {
         Deque<Character> deque=new LinkedList<>();
        for(int i=0;i<s.length();i++){
            if(!deque.isEmpty()&&deque.peek()==s.charAt(i)){
                deque.pop();
            }else{
                deque.push(s.charAt(i));
            }
        }
         StringBuilder builder=new StringBuilder();
        while(!deque.isEmpty()){
            builder.append(deque.pop());
        }
        return builder.reverse().toString();
    }
}

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/127149028