力扣刷题篇之栈与队列篇1(待修改)

系列文章目录


目录

系列文章目录

前言

一、栈的实现

二、单调栈 

总结


前言

本系列是个人力扣刷题汇总,本文是栈与队列。刷题顺序按照[力扣刷题攻略] Re:从零开始的力扣刷题生活 - 力扣(LeetCode)


一、栈的实现

力扣刷题篇之栈与队列篇1(待修改)_第1张图片

力扣刷题篇之栈与队列篇1(待修改)_第2张图片

 两个栈实现一个队列

class MyQueue {
    Stack inStack ; //输入栈
    Stack outStack ;//输出栈

    public MyQueue() {
        inStack = new Stack<>();
        outStack= new Stack<>();
    }
    
    public void push(int x) {
        //往输入栈加入元素即可
        inStack.push(x);
    }
    
    public int pop() {
        if(outStack.isEmpty()){ //将输入栈的元素转移到输出栈
            inToOut();
        }
        //输出栈有元素就得全部输出完,不然顺序无法得到保证
        return outStack.pop();
    }
    
    public int peek() {
         if(outStack.isEmpty()){ //将输入栈的元素转移到输出栈
            inToOut();
        }
        return outStack.peek();
    }
    
    public boolean empty() {
        return inStack.isEmpty()&&outStack.isEmpty();
    }
    //将输入栈的元素转移到输出栈
    public void inToOut(){
        while(!inStack.isEmpty()){
            outStack.push(inStack.pop());
        }
    }
}
/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

 力扣刷题篇之栈与队列篇1(待修改)_第3张图片

 力扣刷题篇之栈与队列篇1(待修改)_第4张图片

 力扣刷题篇之栈与队列篇1(待修改)_第5张图片

 两个队列来实现一个栈

class MyStack {
    private Queue a;//输入队列
    private Queue b;//输出队列
    
    public MyStack() {
        a = new LinkedList<>();
        b = new LinkedList<>();
    }
    
    public void push(int x) {
        a.offer(x);
        // 将b队列中元素全部转给a队列
        while(!b.isEmpty())
            a.offer(b.poll());
        // 交换a和b,使得a队列没有在push()的时候始终为空队列
        Queue temp = a;
        a = b;
        b = temp;
    }
    
    public int pop() {
        return b.poll();
    }
   
    public int top() {
        return b.peek();
    }
    
    public boolean empty() {
        return b.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

力扣刷题篇之栈与队列篇1(待修改)_第6张图片

力扣刷题篇之栈与队列篇1(待修改)_第7张图片

这题看不懂

class TripleInOne {
    private int stackSize;
    private int[] index;
    private int[][] stack;

    public TripleInOne(int stackSize) {
        this.stackSize=stackSize;
        this.index=new int[]{-1,-1,-1};
        this.stack=new int[3][stackSize];
    }
    
    public void push(int stackNum, int value) {
        if(index[stackNum]+1

力扣刷题篇之栈与队列篇1(待修改)_第8张图片力扣刷题篇之栈与队列篇1(待修改)_第9张图片

力扣刷题篇之栈与队列篇1(待修改)_第10张图片

class Solution {
    public List buildArray(int[] target, int n) {
        List res = new ArrayList<>();
        int idx = 1;
        for (int i = 0; i < target.length; ++i) {
            int cur = target[i];
            while (cur > idx) {
                res.add("Push"); res.add("Pop");
                idx++;
            }
            res.add("Push");
            idx++;
        }
        return res;
    }
}

力扣刷题篇之栈与队列篇1(待修改)_第11张图片

二、单调栈 

力扣刷题篇之栈与队列篇1(待修改)_第12张图片

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int M = nums1.length;
        int N = nums2.length;
        int[] answer = new int[M];
        // 使用数组代替map
        int[] map = new int[10001];
        for (int i = 0; i < M; i++) {
            map[nums1[i]] = i + 1;
        }
        int[] stack = new int[N];
        int size = 0;
        // 逆序寻找
        for (int i = N - 1; i >=0; i--) {
            // 弹出栈顶比当前元素小的数
            while (size > 0 && stack[size - 1] < nums2[i]){
                size--;
            }
            // map中含有的才计算
            if(map[nums2[i]] != 0){
                answer[map[nums2[i]] - 1] = size == 0 ? -1 : stack[size - 1];
            }
            stack[size++] = nums2[i];
        }
        return answer;
    }
}

力扣刷题篇之栈与队列篇1(待修改)_第13张图片

力扣刷题篇之栈与队列篇1(待修改)_第14张图片

力扣刷题篇之栈与队列篇1(待修改)_第15张图片

class Solution {
    public int largestRectangleArea(int[] heights) {
        int n = heights.length;
        int[] stack = new int[n + 1];
        int top = 0;
        int res = 0;
        for (int i = 0; i < n; i++) {
            while (top > 0 && heights[i] <= heights[stack[top]]) {
                int temp = stack[top--];
                res = Math.max(res, (i - (top == 0 ? -1 : stack[top]) - 1) * heights[temp]);
            }
            stack[++top] = i;
        }
        while (top > 0) {
            int temp = stack[top--];
            res = Math.max(res, (n - (top == 0 ? -1 : stack[top]) - 1) * heights[temp]);
        }
        return res;
    }
}

力扣刷题篇之栈与队列篇1(待修改)_第16张图片

力扣刷题篇之栈与队列篇1(待修改)_第17张图片

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        int[] dp = new int[n];
        dp[n - 1] = 0;
        for (int i = n - 2; i >= 0; i--) {
            if (temperatures[i] < temperatures[i + 1]) {
                dp[i] = 1;
            } else {
                int t = i + 1;
                while (t < n) {
                    if (temperatures[i] < temperatures[t]) {
                        dp[i] = t - i;
                        break;
                    }
                    if (dp[t] == 0){
                        break;
                    }
                    t = t + dp[t];
                }
            }
        }
        return dp;
    }
}

力扣刷题篇之栈与队列篇1(待修改)_第18张图片

力扣刷题篇之栈与队列篇1(待修改)_第19张图片

// class StockSpanner {

//     List> nums;

//     public StockSpanner() {
//         nums = new ArrayList<>();
//     }

//     public int next(int price) {
//         int n = nums.size(), i = n - 1;
//         while (i >= 0 && price >= nums.get(i).getKey()) {
//             i -= nums.get(i).getValue();
//         }
//         nums.add(new Pair(price, n - i));
//         return n - i;
//     }
// }

// /**
//  * Your StockSpanner object will be instantiated and called as such:
//  * StockSpanner obj = new StockSpanner();
//  * int param_1 = obj.next(price);
//  */

// class StockSpanner {

//     Deque stack;
//     int idx;
//     public StockSpanner() {
//         stack = new ArrayDeque();
//         stack.push(new int[]{-1, Integer.MAX_VALUE});
//         idx = -1; 
//     }
    
//     public int next(int price) {
//         idx ++;
//         while(price >= stack.peek()[1]) {
//             stack.pop();
//         }
//         int res = idx - stack.peek()[0];
//         stack.push(new int[]{idx, price});
//         return res;
//     }
// }

// /**
//  * Your StockSpanner object will be instantiated and called as such:
//  * StockSpanner obj = new StockSpanner();
//  * int param_1 = obj.next(price);
//  */

class StockSpanner {

    Deque stack;
    int idx;
    public StockSpanner() {
        stack = new ArrayDeque();
        stack.push(new int[]{-1, Integer.MAX_VALUE});
        idx = -1; 
    }
    
    public int next(int price) {
        idx ++;
        while(price >= stack.peek()[1]) {
            stack.pop();
        }
        int res = idx - stack.peek()[0];
        stack.push(new int[]{idx, price});
        return res;
    }
}

/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner obj = new StockSpanner();
 * int param_1 = obj.next(price);
 */

力扣刷题篇之栈与队列篇1(待修改)_第20张图片

力扣刷题篇之栈与队列篇1(待修改)_第21张图片

class Solution {
    public int trap(int[] height) {
        // int n = height.length;
		// // left[i]表示i左边的最大值,right[i]表示i右边的最大值
		// int[] left=new int[n];
        // int[] right=new int[n];
		// for (int i = 1; i < n; i++) {
		// 	left[i] = Math.max(left[i - 1], height[i - 1]);
		// }
		// for (int i = n - 2; i >= 0; i--) {
		// 	right[i] = Math.max(right[i + 1], height[i + 1]);
		// }
		// int water = 0;
		// for (int i = 1; i < n; i++) {
		// 	int level = Math.min(left[i], right[i]);
		// 	water += Math.max(0, level - height[i]);
		// }
		// return water;

        int sum = 0;
        int max_left = 0;
        int max_right = 0;
        int left = 1;
        int right = height.length - 2; // 加右指针进去
        for (int i = 1; i < height.length - 1; i++) {
            //从左到右更
            if (height[left - 1] < height[right + 1]) {
                max_left = Math.max(max_left, height[left - 1]);
                // int min = max_left;
                if (max_left > height[left]) {
                    sum = sum + (max_left - height[left]);
                }
                left++;
            //从右到左更
            } else {
                max_right = Math.max(max_right, height[right + 1]);
                // int min = max_right;
                if (max_right > height[right]) {
                    sum = sum + (max_right - height[right]);
                }
                right--;
            }
        }
        return sum;
    }
}


总结

sorry,这几天我估计要降低下更新频率了。

力扣刷题篇之栈与队列篇1(待修改)_第22张图片

你可能感兴趣的:(算法与数据结构,leetcode,leetcode,java,算法)