mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-13 23:55:39 +08:00
Add Swift language blocks to the docs.
This commit is contained in:
@@ -228,6 +228,12 @@ comments: true
|
||||
bool isEmpty = queue.Count() == 0;
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="queue.swift"
|
||||
|
||||
```
|
||||
|
||||
## 队列实现
|
||||
|
||||
队列需要一种可以在一端添加,并在另一端删除的数据结构,也可以使用链表或数组来实现。
|
||||
@@ -612,6 +618,12 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="linkedlist_queue.swift"
|
||||
|
||||
```
|
||||
|
||||
### 基于数组的实现
|
||||
|
||||
数组的删除首元素的时间复杂度为 $O(n)$ ,因此不适合直接用来实现队列。然而,我们可以借助两个指针 `front` , `rear` 来分别记录队首和队尾的索引位置,在入队 / 出队时分别将 `front` / `rear` 向后移动一位即可,这样每次仅需操作一个元素,时间复杂度降至 $O(1)$ 。
|
||||
@@ -1015,6 +1027,12 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="array_queue.swift"
|
||||
|
||||
```
|
||||
|
||||
## 队列典型应用
|
||||
|
||||
- **淘宝订单**。购物者下单后,订单就被加入到队列之中,随后系统再根据顺序依次处理队列中的订单。在双十一时,在短时间内会产生海量的订单,如何处理「高并发」则是工程师们需要重点思考的问题。
|
||||
|
||||
Reference in New Issue
Block a user