剑指offer-栈与队列专题
09.用两个栈实现队列
class CQueue {
LinkedList<Integer> A,B;
public CQueue() {
A=new LinkedList<Integer>();
B=new LinkedList<Integer>();
}
public void appendTail(int value) {
A.addLast(value);
}
public int deleteHead() {
if(!B.isEmpty()) return B.removeLast();
if(A.isEmpty()) return -1;
while(!A.isEmpty()){
B.addLast(A.removeLast());
}
return B.removeLast();
}
}
思考
使用链表模拟栈(Stack)是一种常见的数据结构设计选择,因为链表的结构特点与栈的行为非常相似
链表允许栈的大小动态增长或缩小,而不需要预先分配固定大小的存储空间。这使得链表模拟栈更加灵活,能够适应不同大小的数据集。
链表在头部插入和删除元素的操作非常高效,这正是栈的核心操作(压栈和弹栈)。因此,使用链表模拟栈能够以常数时间复杂度(O(1))执行这些操作,而不会像数组那样需要移动大量元素。
30.包含min函数的栈
class MinStack {
Stack<Integer> A,B;
/** initialize your data structure here. */
public MinStack() {
A=new Stack<Integer>();
B=new Stack<Integer>();
}
public void push(int x) {
A.add(x);
if(B.empty() || B.peek()>=x){
B.add(x);
}
}
public void pop() {
if(A.pop().equals(B.peek()))
B.pop();
}
public int top() {
return A.peek();
}
public int min() {
return B.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/
思考
规定了时间复杂度O1,只能构建辅助栈B存储A的最小元素。Java 代码中,由于 Stack 中存储的是 int 的包装类 Integer ,因此需要使用 equals() 代替 == 来比较值是否相等。
59-I.滑动窗口的最大值
class Solution {
static Comparator<Integer> cmp = new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
//降序
return o2-o1;
}
};
public int[] maxSlidingWindow(int[] nums, int k) {
// 1.暴力超时了O(nk)
// int n=nums.length;
// int[] res=new int[n-k+1];
// int[] window=new int[k];
// for(int i=0;i<n-k+1;i++){
// int max=nums[i];
// for(int j=0;j<k;j++){
// if(nums[i+j]>max) max=nums[i+j];
// }
// res[i]=max;
// }
// return res;
// 2.优先队列O(nlogk)也超时了
// if(nums.length==0) return new int[0];
// PriorityQueue<Integer> queue=new PriorityQueue<Integer>(cmp);
// int[] res=new int[nums.length-k+1];
// for(int i=0;i<k-1;i++) queue.offer(nums[i]);
// for(int i=0;i<res.length;i++){
// queue.offer(nums[i+k-1]);
// res[i]=queue.peek();
// queue.remove(nums[i]);
// }
// return res;
if (nums.length == 0) return nums;
Deque<Integer> deque = new LinkedList<>();
int[] arr = new int[nums.length - k + 1];
int index = 0; //arr数组的下标
//未形成窗口区间
for (int i = 0; i < k; i++) {
//队列不为空时,当前值与队列尾部值比较,如果大于,删除队列尾部值
//一直循环删除到队列中的值都大于当前值,或者删到队列为空
while (!deque.isEmpty() && nums[i] > deque.peekLast()) deque.removeLast();
//执行完上面的循环后,队列中要么为空,要么值都比当前值大,然后就把当前值添加到队列中
deque.addLast(nums[i]);
}
//窗口区间刚形成后,把队列首位值添加到队列中
//因为窗口形成后,就需要把队列首位添加到数组中,而下面的循环是直接跳过这一步的,所以需要我们直接添加
arr[index++] = deque.peekFirst();
//窗口区间形成
for (int i = k; i < nums.length; i++) {
//i-k是已经在区间外了,如果首位等于nums[i-k],那么说明此时首位值已经不再区间内了,需要删除
if (deque.peekFirst() == nums[i - k]) deque.removeFirst();
//删除队列中比当前值大的值
while (!deque.isEmpty() && nums[i] > deque.peekLast()) deque.removeLast();
//把当前值添加到队列中
deque.addLast(nums[i]);
//把队列的首位值添加到arr数组中
arr[index++] = deque.peekFirst();
}
return arr;
}
}
思考
用了几个方法都超时了,看了题解,需要针对不同情况对单调队列进行调整。
59-II.队列的最大值
class MaxQueue {
Queue<Integer> queue;
Deque<Integer> deque;
public MaxQueue() {
queue=new LinkedList<>();
deque=new LinkedList<>();
}
public int max_value() {
if(deque.isEmpty()) return -1;
else return deque.peekFirst();
}
public void push_back(int value) {
queue.offer(value);
while(!deque.isEmpty()&& deque.peekLast()<value){
deque.pollLast();
}
deque.offerLast(value);
}
public int pop_front() {
if(queue.isEmpty()) return -1;
if(queue.peek().equals(deque.peekFirst())){
deque.pollFirst();
}
return queue.poll();
}
}
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue obj = new MaxQueue();
* int param_1 = obj.max_value();
* obj.push_back(value);
* int param_3 = obj.pop_front();
*/
思考
使用双向队列作为辅助,空间换时间。