LeetCode14--用栈实现队列和下一个更大元素I

1.用栈实现队列

//请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty): 
//
// 实现 MyQueue 类: 
//
// 
// void push(int x) 将元素 x 推到队列的末尾 
// int pop() 从队列的开头移除并返回元素 
// int peek() 返回队列开头的元素 
// boolean empty() 如果队列为空,返回 true ;否则,返回 false 
// 
//
// 
//
// 说明: 
//
// 
// 你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
// 
// 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。 
// 
//
// 
//
// 进阶: 
//
// 
// 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。 
// 
//
// 
//
// 示例: 
//
// 
//输入:
//["MyQueue", "push", "push", "peek", "pop", "empty"]
//[[], [1], [2], [], [], []]
//输出:
//[null, null, null, 1, 1, false]
//
//解释:
//MyQueue myQueue = new MyQueue();
//myQueue.push(1); // queue is: [1]
//myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
//myQueue.peek(); // return 1
//myQueue.pop(); // return 1, queue is [2]
//myQueue.empty(); // return false
// 
//
// 
// 
//
// 
//
// 提示: 
//
// 
// 1 <= x <= 9 
// 最多调用 100 次 push、pop、peek 和 empty 
// 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作) 
// 
// Related Topics 栈 设计

这一题和昨天的用队列实现栈是姊妹题,昨天我们采用的方法(两个队列的方法)是将要压栈的元素先放入空队列query2中,随后将query1中的所有元素也放入query2中,现在query1就是一个空的队列了,我们将query2中的值赋给query1,然后将query2变为一个空的队列。同样,这一次我们也采用类似的方法,我们也新建两个栈,我们将stack1中的元素全部压入stack2中,然后我们再将我们需要压入的新元素x也进stack2,这一系列进栈完成之后,我们再将stack2中的元素依次出栈再压入stack1中。

class MyQueue {
    Deque<Integer> stack1;
    Deque<Integer> stack2;
    /** Initialize your data structure here. */
    public MyQueue() {
        stack1 = new LinkedList<Integer>();
        stack2 = new LinkedList<Integer>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        stack1.push(x);
        while(!stack2.isEmpty()){
            stack1.push(stack2.pop());
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack1.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        return stack1.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty();
    }
}

 2.下一个更大元素I

//给定两个 没有重复元素 的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个
//比其大的值。 
//
// nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出 -1 。 
//
// 
//
// 示例 1: 
//
// 输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
//输出: [-1,3,-1]
//解释:
//    对于num1中的数字4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。
//    对于num1中的数字1,第二个数组中数字1右边的下一个较大数字是 3。
//    对于num1中的数字2,第二个数组中没有下一个更大的数字,因此输出 -1。 
//
// 示例 2: 
//
// 输入: nums1 = [2,4], nums2 = [1,2,3,4].
//输出: [3,-1]
//解释:
//    对于 num1 中的数字 2 ,第二个数组中的下一个较大数字是 3 。
//    对于 num1 中的数字 4 ,第二个数组中没有下一个更大的数字,因此输出 -1 。
// 
//
// 
//
// 提示: 
//
// 
// nums1和nums2中所有元素是唯一的。 
// nums1和nums2 的数组大小都不超过1000。 
// 
// Related Topics 栈

这个采用平时数组的方式也可以做,用栈也可以做。用数组就是双指针的一个思想,即找先在num2中遍历找到和num1中相等的元素,随后再在num2中往后遍历,找到比它大的。但是用栈做的话技巧性比较强,就直接看图吧,图上说的一目了然。

 https://leetcode-cn.com/problems/next-greater-element-i/solution/xia-yi-ge-geng-da-yuan-su-i-by-leetcode/

采用数组的方式进行求解:

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Queue<Integer> query = new LinkedList<Integer>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                if(nums1[i]==nums2[j]){
                    if(j==nums2.length-1){
                        query.offer(-1);
                    }else {
                        for (int k = j+1; k < nums2.length; k++) {
                            if(nums2[k] > nums1[i]){
                                query.offer(nums2[k]);
                                break;
                            }
                            if(k == nums2.length-1 && nums1[i]>=nums2[k]){
                                query.offer(-1);
                            }
                        }
                        break;
                    }
                }
            }
        }
        int[] arr = new int[nums1.length];
        int i = 0;
        while(!query.isEmpty()){
            arr[i] = query.poll();
            i++;
        }
        return arr;
    }
}

采用单调栈的方式进行求解

class Solution{
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Deque<Integer> stack = new LinkedList<>();
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[nums1.length];
        for (int i = 0; i < nums2.length; i++) {
            while(!stack.isEmpty() && nums2[i] > stack.peek()){
                map.put(stack.pop(), nums2[i]);
            }
            stack.push(nums2[i]);
        }
        while(!stack.isEmpty()){
            map.put(stack.pop(), -1);
        }
        for (int i = 0; i < nums1.length; i++) {
            res[i] = map.get(nums1[i]);
        }
        return res;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值