LeetCode高频面试60天打卡日记Day01
2020-03-08 13:31
1096 查看
Day01
Idea:
1、维护两个队列 q1接收输入数据 q2作为输出队列
2、push操作,q1接收新来的数据,再将q2中存放的元素全部加入q1,此时q1 = new data + q2 data
3、将q1 和 q2的引用交换,始终保证q1为空接收新来数据
class MyStack { private Queue<Integer> q1; //输入 private Queue<Integer> q2; //输出 private int top; /** Initialize your data structure here. */ public MyStack() { q1 = new LinkedList<Integer>(); q2 = new LinkedList<Integer>(); } /** Push element x onto stack. */ public void push(int x) { q1.offer(x); //q1接收数据,将q2中所有数据复制到q1,保证先来的数据在后面,新来的数据在前面 while(!q2.isEmpty()){ q1.offer(q2.poll()); } Queue temp = q1; q1 = q2; q2 = temp; } /** Removes the element on top of the stack and returns that element. */ public int pop() { return q2.poll(); } /** Get the top element. */ public int top() { return q2.peek(); } /** Returns whether the stack is empty. */ public boolean empty() { return q2.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(); */
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- LeetCode高频面试60天打卡日记Day06
- LeetCode高频面试60天打卡日记Day05
- LeetCode高频面试60天打卡日记Day04
- LeetCode高频面试60天打卡日记Day03
- LeetCode高频面试60天打卡日记Day02
- leetcode-1. Two Sum-简单的算法题,面试见到了吗?
- java面试准备-day01
- ios面试高频题(二)
- 1小时刷完 LeetCode链表题,获取企业面试密钥
- leetcode 刷题日记——Median of Two Sorted Arrays
- 【LeetCode-面试算法经典-Java实现】【119-Pascal's Triangle II(帕斯卡三角形(杨辉三角)II)】
- [置顶] 【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
- leetcode:Restore IP Addresses (修复ip地址) 【面试算法题】
- leetcode top100 面试medium难度
- leetcode 打卡 2018.03.06 #766 Toepliz Matrix
- LeetCode 面试:Add Binary
- 2018.03.07 leetcode 打卡 #169. Majority Element
- leetcode刷题日记——Remove Duplicates from Sorted Array
- [面试专题05]Python全栈日记-判断链表是否存在环
- Maximal Rectangle (求矩阵的最大的子矩阵) 【面试算法leetcode】