Hello everyone, today is the fifth chapter of my leedcode brushing questions, let's take a look together.
Article directory
stack push, pop sequence
The push and pop sequence of Leetcode's stack (difficulty: medium)
Topic requirements
Input two integer sequences, the first sequence represents the push sequence of the stack, please judge whether the second sequence is the pop sequence of the stack. Assume that all numbers pushed onto the stack are unequal. For example, the sequence {1,2,3,4,5} is the push sequence of a certain stack, and the sequence {4,5,3,2,1} is a pop sequence corresponding to the push sequence, but {4,3 ,5,1,2} cannot be the pop sequence of the push sequence.
use case input
Example 1:
Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We can execute in the following order:
push(1), push (2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.
hint
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed 是 popped 的排列。
Question ideas
Let's traverse these two arrays separately. While traversing, first push the pushed array into the stack, and then we judge whether the data on the top of the stack is equal to the data of the popped array. If they are equal, we pop the stack, and the subscript of the popped array is +1. If it is not equal, we will continue to push the data in the pushed array onto the stack. In the loop, if the array is empty at the end, it means that the popped sequence is the popped sequence of the pushed array. If it is not empty, it is not.
At this time, the data at the top of the stack is equal to popped[j], so we pop the data at the top of the stack, and set j++;
the data at the top of the stack is equal to popped[j], and continue to pop.
Continue the operation
The stack is empty, so we return true.
Code
C language code implementation
bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
int* arr = (int*)malloc(pushedSize*sizeof(int));
int tail = 0;
int j = 0;
for(int i = 0; i<pushedSize; i++)
{
arr[tail] = pushed[i];
while(j<poppedSize && tail>=0 && arr[tail] == popped[j])
{
tail--;
j++;
}
tail++;
}
return tail==0;
}
Java code implementation
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int j = 0;
for(int i = 0; i<pushed.length; i++) {
stack.push(pushed[i]);
while( j<popped.length && !stack.empty() && stack.peek().equals(popped[j])) {
stack.pop();
j++;
}
}
return stack.empty();
}
}
minimal stack
The smallest stack of leetcode (difficulty: medium)
Topic requirements
Design a stack that supports push, pop, top operations, and can retrieve the smallest element in constant time.
Implement the MinStack class:
MinStack() Initializes a stack object.
void push(int val) pushes the element val onto the stack.
void pop() Removes the element at the top of the stack.
int top() Gets the top element of the stack.
int getMin() Gets the smallest element in the stack.
This is the interface provided by the scene
class MinStack {
public MinStack() {
}
public void push(int val) {
}
public void pop() {
}
public int top() {
}
public int getMin() {
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
use case input
Example 1:
输入:
[“MinStack”,“push”,“push”,“push”,“getMin”,“pop”,“top”,“getMin”]
[[],[-2],[0],[-3],[],[],[],[]]
输出:
[null,null,null,null,-3,null,0,-2]
Explanation:
MinStack minStack = new MinStack();
myStack.push(-2);
myStack.push(0);
myStack.push(-3);
minStack.getMin(); --> returns -3.
myStack.pop();
myStack.top(); --> 内容 0.
minStack.getMin(); --> returns -2.
hint
-231 <= val <= 231 - 1
pop, top and getMin operations are always called on a non-empty stack.
push, pop, top, and getMin are called at most 3 * 104 times
Question ideas
This question requires us to use two stacks, one stack is used to store all integers, and the top of one stack stores the smallest integer, called the smallest stack. When we push the stack, because the minimum stack is empty when we push it for the first time, we directly push the data into the stack, and when we push the minimum stack later, we need to judge the data that needs to be pushed into the stack Whether it is less than the data at the top of the minimum stack, if it is less than or equal to the stack, it will be pushed into the stack, otherwise it will not be pushed into the stack. When we pop the stack, we also need to make changes to the minimum stack. When the number we pop is equal to the data at the top of the minimum stack, we also pop the data at the top of the minimum stack. I will not describe more about the data returned to the top of the stack later, and the minimum value in the stack.
Repeat this
and then we pop the stack three times
The popped 7 is not equal to -1 at the top of the MinStack stack
-1 is equal to -1 at the top of the MinStack stack, so MinStack also needs to be popped
Code
Because it is more complicated to implement this question in C language, we will implement this question directly in Java.
Java code implementation
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minstack;
public MinStack() {
stack = new Stack<>();
minstack = new Stack<>();
}
public void push(int val) {
stack.push(val);
if(minstack.empty()) {
minstack.push(val);
} else {
if(val <= minstack.peek()) {
minstack.push(val);
}
}
}
public void pop() {
//判断栈是否为空
if(!stack.empty()) {
这里我们用Integer防止取出的数据不在-128~127之间
Integer val = stack.peek();
stack.pop();
if(val.equals(minstack.peek())) {
minstack.pop();
}
}
}
public int top() {
if(!stack.empty()) {
return stack.peek();
}
return -1;
}
public int getMin() {
if(!minstack.empty()) {
return minstack.peek();
}
return -1;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/