mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-10 06:05:35 +08:00
build
This commit is contained in:
@@ -751,19 +751,19 @@ comments: true
|
||||
private var front: ListNode? // 头结点
|
||||
private var rear: ListNode? // 尾结点
|
||||
private var _size = 0
|
||||
|
||||
|
||||
init() {}
|
||||
|
||||
|
||||
/* 获取队列的长度 */
|
||||
func size() -> Int {
|
||||
_size
|
||||
}
|
||||
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
func isEmpty() -> Bool {
|
||||
size() == 0
|
||||
}
|
||||
|
||||
|
||||
/* 入队 */
|
||||
func push(num: Int) {
|
||||
// 尾结点后添加 num
|
||||
@@ -780,7 +780,7 @@ comments: true
|
||||
}
|
||||
_size += 1
|
||||
}
|
||||
|
||||
|
||||
/* 出队 */
|
||||
@discardableResult
|
||||
func poll() -> Int {
|
||||
@@ -790,7 +790,7 @@ comments: true
|
||||
_size -= 1
|
||||
return num
|
||||
}
|
||||
|
||||
|
||||
/* 访问队首元素 */
|
||||
func peek() -> Int {
|
||||
if isEmpty() {
|
||||
@@ -798,6 +798,17 @@ comments: true
|
||||
}
|
||||
return front!.val
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
func toArray() -> [Int] {
|
||||
var node = front
|
||||
var res = Array(repeating: 0, count: size())
|
||||
for i in res.indices {
|
||||
res[i] = node!.val
|
||||
node = node?.next
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1380,6 +1391,16 @@ comments: true
|
||||
}
|
||||
return nums[front]
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
func toArray() -> [Int] {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
var res = Array(repeating: 0, count: queSize)
|
||||
for (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {
|
||||
res[i] = nums[j % capacity()]
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user