mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 00:46:28 +08:00
Merge pull request #250 from nuomi1/feature/deque-Swift
feat: add Swift codes for deque article
This commit is contained in:
commit
56ba30be8f
5 changed files with 73 additions and 1 deletions
|
@ -19,6 +19,7 @@ let package = Package(
|
||||||
.executable(name: "queue", targets: ["queue"]),
|
.executable(name: "queue", targets: ["queue"]),
|
||||||
.executable(name: "linkedlist_queue", targets: ["linkedlist_queue"]),
|
.executable(name: "linkedlist_queue", targets: ["linkedlist_queue"]),
|
||||||
.executable(name: "array_queue", targets: ["array_queue"]),
|
.executable(name: "array_queue", targets: ["array_queue"]),
|
||||||
|
.executable(name: "deque", targets: ["deque"]),
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.target(name: "utils", path: "utils"),
|
.target(name: "utils", path: "utils"),
|
||||||
|
@ -36,5 +37,6 @@ let package = Package(
|
||||||
.executableTarget(name: "queue", path: "chapter_stack_and_queue", sources: ["queue.swift"]),
|
.executableTarget(name: "queue", path: "chapter_stack_and_queue", sources: ["queue.swift"]),
|
||||||
.executableTarget(name: "linkedlist_queue", dependencies: ["utils"], path: "chapter_stack_and_queue", sources: ["linkedlist_queue.swift"]),
|
.executableTarget(name: "linkedlist_queue", dependencies: ["utils"], path: "chapter_stack_and_queue", sources: ["linkedlist_queue.swift"]),
|
||||||
.executableTarget(name: "array_queue", path: "chapter_stack_and_queue", sources: ["array_queue.swift"]),
|
.executableTarget(name: "array_queue", path: "chapter_stack_and_queue", sources: ["array_queue.swift"]),
|
||||||
|
.executableTarget(name: "deque", path: "chapter_stack_and_queue", sources: ["deque.swift"]),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
44
codes/swift/chapter_stack_and_queue/deque.swift
Normal file
44
codes/swift/chapter_stack_and_queue/deque.swift
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/**
|
||||||
|
* File: deque.swift
|
||||||
|
* Created Time: 2023-01-14
|
||||||
|
* Author: nuomi1 (nuomi1@qq.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
@main
|
||||||
|
enum Deque {
|
||||||
|
/* Driver Code */
|
||||||
|
static func main() {
|
||||||
|
/* 初始化双向队列 */
|
||||||
|
// Swift 没有内置的双向队列类,可以把 Array 当作双向队列来使用
|
||||||
|
var deque: [Int] = []
|
||||||
|
|
||||||
|
/* 元素入队 */
|
||||||
|
deque.append(2)
|
||||||
|
deque.append(5)
|
||||||
|
deque.append(4)
|
||||||
|
deque.insert(3, at: 0)
|
||||||
|
deque.insert(1, at: 0)
|
||||||
|
print("双向队列 deque = \(deque)")
|
||||||
|
|
||||||
|
/* 访问元素 */
|
||||||
|
let peekFirst = deque.first!
|
||||||
|
print("队首元素 peekFirst = \(peekFirst)")
|
||||||
|
let peekLast = deque.last!
|
||||||
|
print("队尾元素 peekLast = \(peekLast)")
|
||||||
|
|
||||||
|
/* 元素出队 */
|
||||||
|
// 使用 Array 模拟时 pollFirst 的复杂度为 O(n)
|
||||||
|
let pollFirst = deque.removeFirst()
|
||||||
|
print("队首出队元素 pollFirst = \(pollFirst),队首出队后 deque = \(deque)")
|
||||||
|
let pollLast = deque.removeLast()
|
||||||
|
print("队尾出队元素 pollLast = \(pollLast),队尾出队后 deque = \(deque)")
|
||||||
|
|
||||||
|
/* 获取双向队列的长度 */
|
||||||
|
let size = deque.count
|
||||||
|
print("双向队列长度 size = \(size)")
|
||||||
|
|
||||||
|
/* 判断双向队列是否为空 */
|
||||||
|
let isEmpty = deque.isEmpty
|
||||||
|
print("双向队列是否为空 = \(isEmpty)")
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,6 +25,7 @@ enum Queue {
|
||||||
print("队首元素 peek = \(peek)")
|
print("队首元素 peek = \(peek)")
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
|
// 使用 Array 模拟时 poll 的复杂度为 O(n)
|
||||||
let pool = queue.removeFirst()
|
let pool = queue.removeFirst()
|
||||||
print("出队元素 poll = \(pool),出队后 queue = \(queue)")
|
print("出队元素 poll = \(pool),出队后 queue = \(queue)")
|
||||||
|
|
||||||
|
|
|
@ -196,5 +196,29 @@ comments: true
|
||||||
=== "Swift"
|
=== "Swift"
|
||||||
|
|
||||||
```swift title="deque.swift"
|
```swift title="deque.swift"
|
||||||
|
/* 初始化双向队列 */
|
||||||
|
// Swift 没有内置的双向队列类,可以把 Array 当作双向队列来使用
|
||||||
|
var deque: [Int] = []
|
||||||
|
|
||||||
|
/* 元素入队 */
|
||||||
|
deque.append(2) // 添加至队尾
|
||||||
|
deque.append(5)
|
||||||
|
deque.append(4)
|
||||||
|
deque.insert(3, at: 0) // 添加至队首
|
||||||
|
deque.insert(1, at: 0)
|
||||||
|
|
||||||
|
/* 访问元素 */
|
||||||
|
let peekFirst = deque.first! // 队首元素
|
||||||
|
let peekLast = deque.last! // 队尾元素
|
||||||
|
|
||||||
|
/* 元素出队 */
|
||||||
|
// 使用 Array 模拟时 pollFirst 的复杂度为 O(n)
|
||||||
|
let pollFirst = deque.removeFirst() // 队首元素出队
|
||||||
|
let pollLast = deque.removeLast() // 队尾元素出队
|
||||||
|
|
||||||
|
/* 获取双向队列的长度 */
|
||||||
|
let size = deque.count
|
||||||
|
|
||||||
|
/* 判断双向队列是否为空 */
|
||||||
|
let isEmpty = deque.isEmpty
|
||||||
```
|
```
|
||||||
|
|
|
@ -246,6 +246,7 @@ comments: true
|
||||||
let peek = queue.first!
|
let peek = queue.first!
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
|
// 使用 Array 模拟时 poll 的复杂度为 O(n)
|
||||||
let pool = queue.removeFirst()
|
let pool = queue.removeFirst()
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
|
|
Loading…
Reference in a new issue