在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Implement the following operations of a stack using queues.
Example: MyStack stack = new MyStack(); stack.push(1); stack.push(2); stack.top(); // returns 2 stack.pop(); // returns 2 stack.empty(); // returns false Notes:
使用队列实现栈的下列操作:
注意:
8ms 1 class MyStack { 2 3 var stack: Array<Int> 4 5 /** Initialize your data structure here. */ 6 init() { 7 stack = [] 8 } 9 10 /** Push element x onto stack. */ 11 func push(_ x: Int) { 12 stack.append(x) 13 } 14 15 /** Removes the element on top of the stack and returns that element. */ 16 func pop() -> Int { 17 return stack.removeLast() 18 } 19 20 /** Get the top element. */ 21 func top() -> Int { 22 return stack.last! 23 } 24 25 /** Returns whether the stack is empty. */ 26 func empty() -> Bool { 27 return stack.isEmpty 28 } 29 30 } 31 32 /** 33 * Your MyStack object will be instantiated and called as such: 34 * let obj = MyStack() 35 * obj.push(x) 36 * let ret_2: Int = obj.pop() 37 * let ret_3: Int = obj.top() 38 * let ret_4: Bool = obj.empty() 39 */ 8ms 1 class MyStack { 2 3 fileprivate var array : [Int] 4 5 /** Initialize your data structure here. */ 6 init() { 7 array = [Int]() 8 } 9 10 /** Push element x onto stack. */ 11 func push(_ x: Int) { 12 array.append(x) 13 } 14 15 /** Removes the element on top of the stack and returns that element. */ 16 func pop() -> Int { 17 return array.removeLast() 18 } 19 20 /** Get the top element. */ 21 func top() -> Int { 22 return array.last! 23 } 24 25 /** Returns whether the stack is empty. */ 26 func empty() -> Bool { 27 return array.isEmpty 28 } 29 } 30 31 /** 32 * Your MyStack object will be instantiated and called as such: 33 * let obj = MyStack() 34 * obj.push(x) 35 * let ret_2: Int = obj.pop() 36 * let ret_3: Int = obj.top() 37 * let ret_4: Bool = obj.empty() 38 */ 39 12ms 1 class MyStack { 2 3 private var queue = [Int]() 4 5 /** Initialize your data structure here. */ 6 init() { 7 8 } 9 10 /** Push element x onto stack. */ 11 func push(_ x: Int) { 12 queue.append(x) 13 for _ in 0..<queue.count - 1 { 14 queue.append(queue.removeFirst()) 15 } 16 } 17 18 /** Removes the element on top of the stack and returns that element. */ 19 func pop() -> Int { 20 return queue.removeFirst() 21 } 22 23 /** Get the top element. */ 24 func top() -> Int { 25 return queue.first! 26 } 27 28 /** Returns whether the stack is empty. */ 29 func empty() -> Bool { 30 return queue.isEmpty 31 } 32 } 16ms 1 //: [Previous](@previous) 2 3 import Foundation 4 5 6 class MyQueue { 7 8 var array = Array<Int>() 9 10 /** Initialize your data structure here. */ 11 init() { 12 array = [] 13 } 14 15 /** Push element x to the back of queue. */ 16 func push(_ x: Int) { 17 array.append(x) 18 } 19 20 /** Removes the element from in front of queue and returns that element. */ 21 func pop() -> Int { 22 let last = array.first! 23 array.remove(at: 0) 24 return last 25 } 26 27 /** Get the front element. */ 28 func peek() -> Int { 29 return array.first! 30 } 31 32 /** Returns whether the queue is empty. */ 33 func empty() -> Bool { 34 return array.count == 0 35 } 36 37 func size() -> Int { 38 39 return array.count; 40 } 41 } 42 43 class MyStack { 44 45 var queue: MyQueue? 46 var helpQueue: MyQueue? 47 /** Initialize your data structure here. */ 48 49 init() { 50 queue = MyQueue.init() 51 helpQueue = MyQueue.init() 52 } 53 54 /** Push element x onto stack. */ 55 func push(_ x: Int) { 56 queue?.push(x) 57 } 58 59 /** Removes the element on top of the stack and returns that element. */ 60 func pop() -> Int { 61 62 shift() 63 let popObj = queue?.pop() 64 swap() 65 return popObj! 66 } 67 68 /** Get the top element. */ 69 func top() -> Int { 70 shift() 71 let popObj = queue!.peek() 72 helpQueue?.push(queue!.pop()) 73 swap() 74 return popObj 75 } 76 77 /** Returns whether the stack is empty. */ 78 func empty() -> Bool { 79 80 return queue!.empty() && helpQueue!.empty(); 81 } 82 83 func swap() { 84 85 (queue, helpQueue) = (helpQueue, queue) 86 } 87 88 func shift() { 89 90 while queue?.size() != 1 { 91 92 helpQueue?.push(queue!.pop()) 93 } 94 } 95 96 } 97 98 /** 99 * Your MyStack object will be instantiated and called as such: 100 * let obj = MyStack() 101 * obj.push(x) 102 * let ret_2: Int = obj.pop() 103 * let ret_3: Int = obj.top() 104 * let ret_4: Bool = obj.empty() 105 */
|
请发表评论