队栈和 Hash 的经典算法题

1. 用栈实现队列

力扣 232 题

题目描述
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
● void push(int x) 将元素 x 推到队列的末尾
● int pop() 从队列的开头移除并返回元素
● int peek() 返回队列开头的元素
● boolean empty() 如果队列为空,返回 true ;否则,返回 false
示例 1:

输入:
["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

解题思路
首先想到的栈和队列存储数据的特点分别是,“先进后出”和“先进先出”,也就是说在每次出队时都需要从栈底到栈顶的顺序开始出栈,而栈又只能是从栈顶开始出栈元素。所以我们可以考虑再引入一个栈,让两个栈的栈底相连,每次入队时,依次入右边栈,而出队时,先将右边栈中元素依次入左边栈,然后再从左边栈依次出栈即可

class MyQueue {

    private Deque<Integer> lStack;
    private Deque<Integer> rStack;

    public MyQueue() {
        lStack = new LinkedList<>();
        rStack = new LinkedList<>();
    }

    public void push(int x) {
        rStack.push(x);
    }

    public int pop() {
        if (lStack.isEmpty()) {
            operation();
        }
        return lStack.pop();
    }

    public int peek() {
        if (lStack.isEmpty()) {
            operation();
        }
        return lStack.peek();
    }

    public boolean empty() {
        return lStack.isEmpty() && rStack.isEmpty();
    }

    private void operation() {
        while (!rStack.isEmpty()) {
            lStack.push(rStack.pop());
        }
    }
}

2. 利用队列实现栈

力扣 225 题

题目描述
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
● void push(int x) 将元素 x 压入栈顶。
● int pop() 移除并返回栈顶元素。
● int top() 返回栈顶元素。
● boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
● 你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
● 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

解题思路
利用两个队列进行实现,在元素做入栈操作时,可以先将元素入队 queue2 中,然后将 queue1 中的元素再依次入到队列 queue2 中,再交换 queue1 和 queue2 的引用,最后得以实现后入队的元素在 queue1 的最前面

class MyStack {
    
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }

    public int pop() {
        return queue1.poll();
    }

    public int top() {
        return queue1.peek();
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

3. n 数之和专题

力扣 | 第 1 题

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6
输出:[1,2]

示例 3:

输入:nums = [3,3], target = 6
输出:[0,1]

解题思路
利用 HashMap<k, v> 进行解决,将两个加数作为键 k,索引作为值 v,通过 for 循环遍历数组,依次匹配

public int[] twoSum(int[] nums, int target) {
    HashMap<Integer, Integer> map = new HashMap<>();
    int[] res = new int[2];
    for (int i = 0; i < nums.length; i++) {
        int flag = target - nums[i];
        if (map.containsKey(flag)) {
            res[0] = i;
            res[1] = map.get(flag);
        }
        map.put(nums[i], i);
    }
    return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傻笑不累

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值