mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 22:16:06 +00:00
Review Swift codes (#1150)
* feat(swift): review for chapter_computational_complexity * feat(swift): review for chapter_data_structure * feat(swift): review for chapter_array_and_linkedlist * feat(swift): review for chapter_stack_and_queue * feat(swift): review for chapter_hashing * feat(swift): review for chapter_tree * feat(swift): add codes for heap article * feat(swift): review for chapter_heap * feat(swift): review for chapter_graph * feat(swift): review for chapter_searching * feat(swift): review for chapter_sorting * feat(swift): review for chapter_divide_and_conquer * feat(swift): review for chapter_backtracking * feat(swift): review for chapter_dynamic_programming * feat(swift): review for chapter_greedy * feat(swift): review for utils * feat(swift): update ci tool * feat(swift): trailing closure * feat(swift): array init * feat(swift): map index
This commit is contained in:
@@ -54,6 +54,11 @@ func traverse(nums: [Int]) {
|
||||
for num in nums {
|
||||
count += num
|
||||
}
|
||||
// 同时遍历数据索引和元素
|
||||
for (i, num) in nums.enumerated() {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
|
||||
@@ -22,7 +22,6 @@ func remove(n0: ListNode) {
|
||||
let P = n0.next
|
||||
let n1 = P?.next
|
||||
n0.next = n1
|
||||
P?.next = nil
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
|
||||
@@ -7,12 +7,15 @@
|
||||
/* 列表类 */
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -29,7 +32,7 @@ class MyList {
|
||||
/* 访问元素 */
|
||||
func get(index: Int) -> Int {
|
||||
// 索引如果越界则抛出错误,下同
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
return arr[index]
|
||||
@@ -37,7 +40,7 @@ class MyList {
|
||||
|
||||
/* 更新元素 */
|
||||
func set(index: Int, num: Int) {
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
arr[index] = num
|
||||
@@ -46,25 +49,25 @@ class MyList {
|
||||
/* 在尾部添加元素 */
|
||||
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
|
||||
@@ -75,12 +78,12 @@ class MyList {
|
||||
/* 删除元素 */
|
||||
@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]
|
||||
}
|
||||
// 更新元素数量
|
||||
@@ -92,18 +95,14 @@ class MyList {
|
||||
/* 列表扩容 */
|
||||
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