Merge pull request #218 from nuomi1/feature/linked_list-Swift

feat: add Swift codes for linked_list article
This commit is contained in:
Yudong Jin
2023-01-09 02:08:43 +08:00
committed by GitHub
11 changed files with 244 additions and 73 deletions

View File

@@ -10,6 +10,7 @@ let package = Package(
.executable(name: "space_complexity", targets: ["space_complexity"]),
.executable(name: "leetcode_two_sum", targets: ["leetcode_two_sum"]),
.executable(name: "array", targets: ["array"]),
.executable(name: "linked_list", targets: ["linked_list"]),
],
targets: [
.target(name: "utils", path: "utils"),
@@ -18,5 +19,6 @@ let package = Package(
.executableTarget(name: "space_complexity", dependencies: ["utils"], path: "chapter_computational_complexity", sources: ["space_complexity.swift"]),
.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"]),
]
)

View File

@@ -0,0 +1,91 @@
/**
* File: linked_list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* n0 P */
func insert(n0: ListNode, P: ListNode) {
let n1 = n0.next
n0.next = P
P.next = n1
}
/* n0 */
func remove(n0: ListNode) {
if n0.next == nil {
return
}
// n0 -> P -> n1
let P = n0.next
let n1 = P?.next
n0.next = n1
P?.next = nil
}
/* 访 index */
func access(head: ListNode, index: Int) -> ListNode? {
var head: ListNode? = head
for _ in 0 ..< index {
head = head?.next
if head == nil {
return nil
}
}
return head
}
/* target */
func find(head: ListNode, target: Int) -> Int {
var head: ListNode? = head
var index = 0
while head != nil {
if head?.val == target {
return index
}
head = head?.next
index += 1
}
return -1
}
@main
enum LinkedList {
/* Driver Code */
static func main() {
/* */
//
let n0 = ListNode(x: 1)
let n1 = ListNode(x: 3)
let n2 = ListNode(x: 2)
let n3 = ListNode(x: 5)
let n4 = ListNode(x: 4)
//
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
print("初始化的链表为")
PrintUtil.printLinkedList(head: n0)
/* */
insert(n0: n0, P: ListNode(x: 0))
print("插入结点后的链表为")
PrintUtil.printLinkedList(head: n0)
/* */
remove(n0: n0)
print("删除结点后的链表为")
PrintUtil.printLinkedList(head: n0)
/* 访 */
let node = access(head: n0, index: 3)
print("链表中索引 3 处的结点的值 = \(node!.val)")
/* */
let index = find(head: n0, target: 2)
print("链表中值为 2 的结点的索引 = \(index)")
}
}

View File

@@ -6,14 +6,14 @@
import utils
//
/* */
@discardableResult
func function() -> Int {
// do something
return 0
}
//
/* */
func constant(n: Int) {
// O(1)
let a = 0
@@ -30,7 +30,7 @@ func constant(n: Int) {
}
}
// 线
/* 线 */
func linear(n: Int) {
// n O(n)
let nums = Array(repeating: 0, count: n)
@@ -40,7 +40,7 @@ func linear(n: Int) {
let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, "\($0)") })
}
// 线
/* 线 */
func linearRecur(n: Int) {
print("递归 n = \(n)")
if n == 1 {
@@ -49,13 +49,13 @@ func linearRecur(n: Int) {
linearRecur(n: n - 1)
}
//
/* */
func quadratic(n: Int) {
// O(n^2)
let numList = Array(repeating: Array(repeating: 0, count: n), count: n)
}
//
/* */
@discardableResult
func quadraticRecur(n: Int) -> Int {
if n <= 0 {
@@ -67,7 +67,7 @@ func quadraticRecur(n: Int) -> Int {
return quadraticRecur(n: n - 1)
}
//
/* */
func buildTree(n: Int) -> TreeNode? {
if n == 0 {
return nil
@@ -80,7 +80,7 @@ func buildTree(n: Int) -> TreeNode? {
@main
enum SpaceComplexity {
// Driver Code
/* Driver Code */
static func main() {
let n = 5
//

View File

@@ -4,7 +4,7 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
//
/* */
func constant(n: Int) -> Int {
var count = 0
let size = 100_000
@@ -14,7 +14,7 @@ func constant(n: Int) -> Int {
return count
}
// 线
/* 线 */
func linear(n: Int) -> Int {
var count = 0
for _ in 0 ..< n {
@@ -23,7 +23,7 @@ func linear(n: Int) -> Int {
return count
}
// 线
/* 线 */
func arrayTraversal(nums: [Int]) -> Int {
var count = 0
//
@@ -33,7 +33,7 @@ func arrayTraversal(nums: [Int]) -> Int {
return count
}
//
/* */
func quadratic(n: Int) -> Int {
var count = 0
//
@@ -45,7 +45,7 @@ func quadratic(n: Int) -> Int {
return count
}
//
/* */
func bubbleSort(nums: inout [Int]) -> Int {
var count = 0 //
// n-1, n-2, ..., 1
@@ -64,7 +64,7 @@ func bubbleSort(nums: inout [Int]) -> Int {
return count
}
//
/* */
func exponential(n: Int) -> Int {
var count = 0
var base = 1
@@ -79,7 +79,7 @@ func exponential(n: Int) -> Int {
return count
}
//
/* */
func expRecur(n: Int) -> Int {
if n == 1 {
return 1
@@ -87,7 +87,7 @@ func expRecur(n: Int) -> Int {
return expRecur(n: n - 1) + expRecur(n: n - 1) + 1
}
//
/* */
func logarithmic(n: Int) -> Int {
var count = 0
var n = n
@@ -98,7 +98,7 @@ func logarithmic(n: Int) -> Int {
return count
}
//
/* */
func logRecur(n: Int) -> Int {
if n <= 1 {
return 0
@@ -106,7 +106,7 @@ func logRecur(n: Int) -> Int {
return logRecur(n: n / 2) + 1
}
// 线
/* 线 */
func linearLogRecur(n: Double) -> Int {
if n <= 1 {
return 1
@@ -118,7 +118,7 @@ func linearLogRecur(n: Double) -> Int {
return count
}
//
/* */
func factorialRecur(n: Int) -> Int {
if n == 0 {
return 1
@@ -133,39 +133,40 @@ func factorialRecur(n: Int) -> Int {
@main
enum TimeComplexity {
/* Driver Code */
static func main() {
// n
let n = 8
print("输入数据大小 n =", n)
print("输入数据大小 n = \(n)")
var count = constant(n: n)
print("常数阶的计算操作数量 =", count)
print("常数阶的计算操作数量 = \(count)")
count = linear(n: n)
print("线性阶的计算操作数量 =", count)
print("线性阶的计算操作数量 = \(count)")
count = arrayTraversal(nums: Array(repeating: 0, count: n))
print("线性阶(遍历数组)的计算操作数量 =", count)
print("线性阶(遍历数组)的计算操作数量 = \(count)")
count = quadratic(n: n)
print("平方阶的计算操作数量 =", count)
print("平方阶的计算操作数量 = \(count)")
var nums = Array(sequence(first: n, next: { $0 > 0 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
count = bubbleSort(nums: &nums)
print("平方阶(冒泡排序)的计算操作数量 =", count)
print("平方阶(冒泡排序)的计算操作数量 = \(count)")
count = exponential(n: n)
print("指数阶(循环实现)的计算操作数量 =", count)
print("指数阶(循环实现)的计算操作数量 = \(count)")
count = expRecur(n: n)
print("指数阶(递归实现)的计算操作数量 =", count)
print("指数阶(递归实现)的计算操作数量 = \(count)")
count = logarithmic(n: n)
print("对数阶(循环实现)的计算操作数量 =", count)
print("对数阶(循环实现)的计算操作数量 = \(count)")
count = logRecur(n: n)
print("对数阶(递归实现)的计算操作数量 =", count)
print("对数阶(递归实现)的计算操作数量 = \(count)")
count = linearLogRecur(n: Double(n))
print("线性对数阶(递归实现)的计算操作数量 =", count)
print("线性对数阶(递归实现)的计算操作数量 = \(count)")
count = factorialRecur(n: n)
print("阶乘阶(递归实现)的计算操作数量 =", count)
print("阶乘阶(递归实现)的计算操作数量 = \(count)")
}
}

View File

@@ -4,7 +4,7 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
// { 1, 2, ..., n }
/* { 1, 2, ..., n } */
func randomNumbers(n: Int) -> [Int] {
// nums = { 1, 2, 3, ..., n }
var nums = Array(1 ... n)
@@ -13,7 +13,7 @@ func randomNumbers(n: Int) -> [Int] {
return nums
}
// nums 1
/* nums 1 */
func findOne(nums: [Int]) -> Int {
for i in nums.indices {
if nums[i] == 1 {
@@ -25,14 +25,14 @@ func findOne(nums: [Int]) -> Int {
@main
enum WorstBestTimeComplexity {
// Driver Code
/* Driver Code */
static func main() {
for _ in 0 ..< 10 {
let n = 100
let nums = randomNumbers(n: n)
let index = findOne(nums: nums)
print("数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
print("数字 1 的索引为", index)
print("数组 [ 1, 2, ..., n ] 被打乱后 = \(nums)")
print("数字 1 的索引为 \(index)")
}
}
}

View File

@@ -15,6 +15,16 @@ public enum PrintUtil {
}
}
public static func printLinkedList(head: ListNode) {
var head: ListNode? = head
var list: [String] = []
while head != nil {
list.append("\(head!.val)")
head = head?.next
}
print(list.joined(separator: " -> "))
}
public static func printTree(root: TreeNode?) {
printTree(root: root, prev: nil, isLeft: false)
}