mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -598,5 +598,124 @@ MyStack.prototype.empty = function() {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Swift
|
||||||
|
```Swift
|
||||||
|
// 定义一个队列数据结构
|
||||||
|
class Queue {
|
||||||
|
var array: [Int]
|
||||||
|
init() {
|
||||||
|
array = [Int]()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Push element x to the back of queue. */
|
||||||
|
func push(_ x: Int) {
|
||||||
|
array.append(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Removes the element from in front of queue and returns that element. */
|
||||||
|
func pop() -> Int {
|
||||||
|
if array.isEmpty {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return array.removeFirst()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the front element. */
|
||||||
|
func peek() -> Int {
|
||||||
|
if array.isEmpty {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return array.first!
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns whether the queue is empty. */
|
||||||
|
func empty() -> Bool {
|
||||||
|
return array.isEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
func count() -> Int {
|
||||||
|
return array.count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用双队列
|
||||||
|
class MyStack {
|
||||||
|
var queue1: Queue
|
||||||
|
var queue2: Queue
|
||||||
|
|
||||||
|
init() {
|
||||||
|
queue1 = Queue()
|
||||||
|
queue2 = Queue()
|
||||||
|
}
|
||||||
|
|
||||||
|
func push(_ x: Int) {
|
||||||
|
queue1.push(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pop() -> Int {
|
||||||
|
if queue1.empty() {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
while queue1.count() > 1 {
|
||||||
|
queue2.push(queue1.pop())
|
||||||
|
}
|
||||||
|
let res = queue1.pop()
|
||||||
|
while !queue2.empty() {
|
||||||
|
queue1.push(queue2.pop())
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func top() -> Int {
|
||||||
|
if queue1.empty() {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
let res = pop()
|
||||||
|
push(res)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func empty() -> Bool {
|
||||||
|
return queue1.empty() && queue2.empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用单队列
|
||||||
|
class MyStack {
|
||||||
|
var queue: Queue
|
||||||
|
|
||||||
|
init() {
|
||||||
|
queue = Queue()
|
||||||
|
}
|
||||||
|
|
||||||
|
func push(_ x: Int) {
|
||||||
|
queue.push(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pop() -> Int {
|
||||||
|
if queue.empty() {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
for _ in 1 ..< queue.count() {
|
||||||
|
queue.push(queue.pop())
|
||||||
|
}
|
||||||
|
return queue.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func top() -> Int {
|
||||||
|
if queue.empty() {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
let res = pop()
|
||||||
|
push(res)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func empty() -> Bool {
|
||||||
|
return queue.empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -349,6 +349,44 @@ MyQueue.prototype.empty = function() {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Swift:
|
||||||
|
```swift
|
||||||
|
class MyQueue {
|
||||||
|
|
||||||
|
var stackIn = [Int]()
|
||||||
|
var stackOut = [Int]()
|
||||||
|
|
||||||
|
init() {}
|
||||||
|
|
||||||
|
/** Push element x to the back of queue. */
|
||||||
|
func push(_ x: Int) {
|
||||||
|
stackIn.append(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Removes the element from in front of queue and returns that element. */
|
||||||
|
func pop() -> Int {
|
||||||
|
if stackOut.isEmpty {
|
||||||
|
while !stackIn.isEmpty {
|
||||||
|
stackOut.append(stackIn.popLast()!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stackOut.popLast() ?? -1
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the front element. */
|
||||||
|
func peek() -> Int {
|
||||||
|
let res = pop()
|
||||||
|
stackOut.append(res)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns whether the queue is empty. */
|
||||||
|
func empty() -> Bool {
|
||||||
|
return stackIn.isEmpty && stackOut.isEmpty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
C:
|
C:
|
||||||
```C
|
```C
|
||||||
/*
|
/*
|
||||||
@ -426,6 +464,5 @@ void myQueueFree(MyQueue* obj) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user