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:
nuomi1
2024-03-20 21:15:39 +08:00
committed by GitHub
parent 300a781fab
commit 7359a7cb4b
55 changed files with 293 additions and 224 deletions
@@ -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()))
}
}