mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-03 07:01:55 +08:00
parent
7ce7386bab
commit
a0ee691475
@ -38,10 +38,10 @@ class ArrayQueue {
|
|||||||
}
|
}
|
||||||
// 计算尾指针,指向队尾索引 + 1
|
// 计算尾指针,指向队尾索引 + 1
|
||||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||||
int rear = (front + queSize) % capacity();
|
let rear = (front + queSize) % capacity()
|
||||||
// 尾结点后添加 num
|
// 尾结点后添加 num
|
||||||
nums[rear] = num;
|
nums[rear] = num
|
||||||
queSize++;
|
queSize += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 出队 */
|
/* 出队 */
|
||||||
@ -49,9 +49,9 @@ class ArrayQueue {
|
|||||||
func poll() -> Int {
|
func poll() -> Int {
|
||||||
let num = peek()
|
let num = peek()
|
||||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||||
front = (front + 1) % capacity();
|
front = (front + 1) % capacity()
|
||||||
queSize--;
|
queSize -= 1
|
||||||
return num;
|
return num
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
@ -66,7 +66,7 @@ class ArrayQueue {
|
|||||||
func toArray() -> [Int] {
|
func toArray() -> [Int] {
|
||||||
// 仅转换有效长度范围内的列表元素
|
// 仅转换有效长度范围内的列表元素
|
||||||
var res = Array(repeating: 0, count: queSize)
|
var res = Array(repeating: 0, count: queSize)
|
||||||
for (i, j) in sequence(first: (0, front), next: { $0 < queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {
|
for (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {
|
||||||
res[i] = nums[j % capacity()]
|
res[i] = nums[j % capacity()]
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
@ -1227,10 +1227,10 @@ comments: true
|
|||||||
}
|
}
|
||||||
// 计算尾指针,指向队尾索引 + 1
|
// 计算尾指针,指向队尾索引 + 1
|
||||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||||
int rear = (front + queSize) % capacity();
|
let rear = (front + queSize) % capacity()
|
||||||
// 尾结点后添加 num
|
// 尾结点后添加 num
|
||||||
nums[rear] = num;
|
nums[rear] = num
|
||||||
queSize++;
|
queSize += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 出队 */
|
/* 出队 */
|
||||||
@ -1238,9 +1238,9 @@ comments: true
|
|||||||
func poll() -> Int {
|
func poll() -> Int {
|
||||||
let num = peek()
|
let num = peek()
|
||||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||||
front = (front + 1) % capacity();
|
front = (front + 1) % capacity()
|
||||||
queSize--;
|
queSize -= 1
|
||||||
return num;
|
return num
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
|
Loading…
Reference in New Issue
Block a user