使用队列实现栈的下列操作:
- push(x) -- 元素 x 入栈
- pop() -- 移除栈顶元素
- top() -- 获取栈顶元素
- empty() -- 返回栈是否为空
注意:
- 你只能使用队列的基本操作-- 也就是
push to back
,peek/pop from front
,size
, 和is empty
这些操作是合法的。 - 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
- 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
超级慢的。
1 class MyStack { 2 private LinkedList<Integer> list1 ; 3 private LinkedList<Integer> list2 ; 4 /** Initialize your data structure here. */ 5 public MyStack() { 6 this.list1 = new LinkedList<Integer>(); 7 this.list2 = new LinkedList<Integer>(); 8 } 9 10 /** Push element x onto stack. */ 11 public void push(int x) { 12 list1.add(x); 13 } 14 15 /** Removes the element on top of the stack and returns that element. */ 16 public int pop() { 17 int res = 0; 18 if(list1.size()==1){ 19 return list1.poll(); 20 }else{ 21 while(list1.size()!=1){ 22 list2.add(list1.poll()); 23 } 24 res = list1.poll(); 25 while(!list2.isEmpty()){ 26 list1.add(list2.poll()); 27 } 28 } 29 return res; 30 } 31 32 /** Get the top element. */ 33 public int top() { 34 int res = 0; 35 if(list1.size() == 1){ 36 res = list1.peek(); 37 38 }else{ 39 while(list1.size()!=1){ 40 list2.add(list1.poll()); 41 } 42 res = list1.peek(); 43 list2.add(list1.poll()); 44 while(!list2.isEmpty()){ 45 list1.add(list2.poll()); 46 } 47 } 48 return res; 49 } 50 51 /** Returns whether the stack is empty. */ 52 public boolean empty() { 53 if(list1.isEmpty() && list2.isEmpty()){ 54 return true; 55 }else{ 56 return false; 57 } 58 } 59 } 60 61 /** 62 * Your MyStack object will be instantiated and called as such: 63 * MyStack obj = new MyStack(); 64 * obj.push(x); 65 * int param_2 = obj.pop(); 66 * int param_3 = obj.top(); 67 * boolean param_4 = obj.empty(); 68 */
这个比较快:
1 class MyStack { 2 3 List<Integer> queue1=new LinkedList(); 4 List<Integer> queue2=new LinkedList(); 5 private int num=1; 6 /** Initialize your data structure here. */ 7 public MyStack() { 8 9 } 10 11 /** Push element x onto stack. */ 12 public void push(int x) { 13 if(num==1) { 14 queue1.add(x); 15 }else { 16 queue2.add(x); 17 } 18 } 19 20 /** Removes the element on top of the stack and returns that element. */ 21 public int pop() { 22 if(num==1) { 23 while(queue1.size()!=1) { 24 queue2.add(queue1.remove(0)); 25 } 26 num=2; 27 return queue1.remove(0); 28 }else { 29 while(queue2.size()!=1) { 30 queue1.add(queue2.remove(0)); 31 } 32 num=1; 33 return queue2.remove(0); 34 } 35 } 36 37 /** Get the top element. */ 38 public int top() { 39 if(num==1) { 40 return queue1.get(queue1.size()-1); 41 }else { 42 return queue2.get(queue2.size()-1); 43 } 44 } 45 46 /** Returns whether the stack is empty. */ 47 public boolean empty() { 48 if(num==1) { 49 return queue1.isEmpty(); 50 }else { 51 return queue2.isEmpty(); 52 } 53 } 54 } 55 56 /** 57 * Your MyStack object will be instantiated and called as such: 58 * MyStack obj = new MyStack(); 59 * obj.push(x); 60 * int param_2 = obj.pop(); 61 * int param_3 = obj.top(); 62 * boolean param_4 = obj.empty(); 63 */
最简单:
1 class MyStack { 2 private Queue<Integer> data; 3 4 public MyStack() { 5 data = new LinkedList<>(); 6 } 7 8 public void push(int x) { 9 data.offer(x); 10 //将队列中前面已经逆序的元素放在x元素后面,使得整体逆序 11 for (int i = 0; i < data.size() - 1; i++) { 12 data.offer(data.poll()); 13 } 14 } 15 16 public int pop() { 17 return data.poll(); 18 } 19 20 public int top() { 21 return data.peek(); 22 } 23 24 public boolean empty() { 25 return data.isEmpty(); 26 } 27 } 28 29 /** 30 * Your MyStack object will be instantiated and called as such: 31 * MyStack obj = new MyStack(); 32 * obj.push(x); 33 * int param_2 = obj.pop(); 34 * int param_3 = obj.top(); 35 * boolean param_4 = obj.empty(); 36 */
2019-03-04 23:00:41