mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-22 07:24:23 +08:00
build
This commit is contained in:
@ -750,6 +750,11 @@ comments: true
|
||||
for num in nums {
|
||||
count += num
|
||||
}
|
||||
// 同时遍历数据索引和元素
|
||||
for (i, num) in nums.enumerated() {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -644,7 +644,6 @@ comments: true
|
||||
let P = n0.next
|
||||
let n1 = P?.next
|
||||
n0.next = n1
|
||||
P?.next = nil
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1412,12 +1412,15 @@ comments: true
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
private var arr: [Int] // 数组(存储列表元素)
|
||||
private var _capacity = 10 // 列表容量
|
||||
private var _size = 0 // 列表长度(当前元素数量)
|
||||
private let extendRatio = 2 // 每次列表扩容的倍数
|
||||
private var _capacity: Int // 列表容量
|
||||
private var _size: Int // 列表长度(当前元素数量)
|
||||
private let extendRatio: Int // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
init() {
|
||||
_capacity = 10
|
||||
_size = 0
|
||||
extendRatio = 2
|
||||
arr = Array(repeating: 0, count: _capacity)
|
||||
}
|
||||
|
||||
@ -1434,7 +1437,7 @@ comments: true
|
||||
/* 访问元素 */
|
||||
func get(index: Int) -> Int {
|
||||
// 索引如果越界则抛出错误,下同
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
return arr[index]
|
||||
@ -1442,7 +1445,7 @@ comments: true
|
||||
|
||||
/* 更新元素 */
|
||||
func set(index: Int, num: Int) {
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
arr[index] = num
|
||||
@ -1451,25 +1454,25 @@ comments: true
|
||||
/* 在尾部添加元素 */
|
||||
func add(num: Int) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if _size == _capacity {
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
arr[_size] = num
|
||||
arr[size()] = num
|
||||
// 更新元素数量
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* 在中间插入元素 */
|
||||
func insert(index: Int, num: Int) {
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if _size == _capacity {
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {
|
||||
for j in (index ..< size()).reversed() {
|
||||
arr[j + 1] = arr[j]
|
||||
}
|
||||
arr[index] = num
|
||||
@ -1480,12 +1483,12 @@ comments: true
|
||||
/* 删除元素 */
|
||||
@discardableResult
|
||||
func remove(index: Int) -> Int {
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
let num = arr[index]
|
||||
// 将将索引 index 之后的元素都向前移动一位
|
||||
for j in index ..< (_size - 1) {
|
||||
for j in index ..< (size() - 1) {
|
||||
arr[j] = arr[j + 1]
|
||||
}
|
||||
// 更新元素数量
|
||||
@ -1497,18 +1500,14 @@ comments: true
|
||||
/* 列表扩容 */
|
||||
func extendCapacity() {
|
||||
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
||||
arr = arr + Array(repeating: 0, count: _capacity * (extendRatio - 1))
|
||||
arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))
|
||||
// 更新列表容量
|
||||
_capacity = arr.count
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
func toArray() -> [Int] {
|
||||
var arr = Array(repeating: 0, count: _size)
|
||||
for i in 0 ..< _size {
|
||||
arr[i] = get(index: i)
|
||||
}
|
||||
return arr
|
||||
Array(arr.prefix(size()))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user