feat: add Swift codes for list article

This commit is contained in:
nuomi1
2023-01-08 23:44:13 +08:00
parent 97ee638d31
commit 5e23c75870
4 changed files with 354 additions and 4 deletions

View File

@ -11,6 +11,8 @@ let package = Package(
.executable(name: "leetcode_two_sum", targets: ["leetcode_two_sum"]),
.executable(name: "array", targets: ["array"]),
.executable(name: "linked_list", targets: ["linked_list"]),
.executable(name: "list", targets: ["list"]),
.executable(name: "my_list", targets: ["my_list"]),
],
targets: [
.target(name: "utils", path: "utils"),
@ -20,5 +22,7 @@ let package = Package(
.executableTarget(name: "leetcode_two_sum", path: "chapter_computational_complexity", sources: ["leetcode_two_sum.swift"]),
.executableTarget(name: "array", path: "chapter_array_and_linkedlist", sources: ["array.swift"]),
.executableTarget(name: "linked_list", dependencies: ["utils"], path: "chapter_array_and_linkedlist", sources: ["linked_list.swift"]),
.executableTarget(name: "list", path: "chapter_array_and_linkedlist", sources: ["list.swift"]),
.executableTarget(name: "my_list", path: "chapter_array_and_linkedlist", sources: ["my_list.swift"]),
]
)

View File

@ -0,0 +1,64 @@
/**
* File: list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum List {
/* Driver Code */
static func main() {
/* */
var list = [1, 3, 2, 5, 4]
print("列表 list = \(list)")
/* 访 */
let num = list[1]
print("访问索引 1 处的元素,得到 num = \(num)")
/* */
list[1] = 0
print("将索引 1 处的元素更新为 0 ,得到 list = \(list)")
/* */
list.removeAll()
print("清空列表后 list = \(list)")
/* */
list.append(1)
list.append(3)
list.append(2)
list.append(5)
list.append(4)
print("添加元素后 list = \(list)")
/* */
list.insert(6, at: 3)
print("在索引 3 处插入数字 6 ,得到 list = \(list)")
/* */
list.remove(at: 3)
print("删除索引 3 处的元素,得到 list = \(list)")
/* */
var count = 0
for _ in list.indices {
count += 1
}
/* */
count = 0
for _ in list {
count += 1
}
/* */
let list1 = [6, 8, 7, 10, 9]
list.append(contentsOf: list1)
print("将列表 list1 拼接到 list 之后,得到 list = \(list)")
/* */
list.sort()
print("排序列表后 list = \(list)")
}
}

View File

@ -0,0 +1,147 @@
/**
* File: my_list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
class MyList {
private var nums: [Int] //
private var _capacity = 10 //
private var _size = 0 //
private let extendRatio = 2 //
/* */
init() {
nums = Array(repeating: 0, count: _capacity)
}
/* */
func size() -> Int {
_size
}
/* */
func capacity() -> Int {
_capacity
}
/* 访 */
func get(index: Int) -> Int {
//
if index >= _size {
fatalError("索引越界")
}
return nums[index]
}
/* */
func set(index: Int, num: Int) {
if index >= _size {
fatalError("索引越界")
}
nums[index] = num
}
/* */
func add(num: Int) {
//
if _size == _capacity {
extendCapacity()
}
nums[_size] = num
//
_size += 1
}
/* */
func insert(index: Int, num: Int) {
if index >= _size {
fatalError("索引越界")
}
//
if _size == _capacity {
extendCapacity()
}
// index
for j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {
nums[j + 1] = nums[j]
}
nums[index] = num
//
_size += 1
}
/* */
@discardableResult
func remove(index: Int) -> Int {
if index >= _size {
fatalError("索引越界")
}
let num = nums[index]
// index
for j in index ..< (_size - 1) {
nums[j] = nums[j + 1]
}
//
_size -= 1
//
return num
}
/* */
func extendCapacity() {
// size
nums = nums + Array(repeating: 0, count: _capacity * (extendRatio - 1))
//
_capacity = nums.count
}
/* */
func toArray() -> [Int] {
var nums = Array(repeating: 0, count: _size)
for i in 0 ..< _size {
nums[i] = get(index: i)
}
return nums
}
}
@main
enum _MyList {
/* Driver Code */
static func main() {
/* */
let list = MyList()
/* */
list.add(num: 1)
list.add(num: 3)
list.add(num: 2)
list.add(num: 5)
list.add(num: 4)
print("列表 list = \(list.toArray()) ,容量 = \(list.capacity()) ,长度 = \(list.size())")
/* */
list.insert(index: 3, num: 6)
print("在索引 3 处插入数字 6 ,得到 list = \(list.toArray())")
/* */
list.remove(index: 3)
print("删除索引 3 处的元素,得到 list = \(list.toArray())")
/* 访 */
let num = list.get(index: 1)
print("访问索引 1 处的元素,得到 num = \(num)")
/* */
list.set(index: 1, num: 0)
print("将索引 1 处的元素更新为 0 ,得到 list = \(list.toArray())")
/* */
for i in 0 ..< 10 {
// i = 5
list.add(num: i)
}
print("扩容后的列表 list = \(list.toArray()) ,容量 = \(list.capacity()) ,长度 = \(list.size())")
}
}