Skip to content

Commit d1ed176

Browse files
Merge pull request wangzheng0822#50 from JiandanDream/master
[add] Add the version of Swift.
2 parents 43b267f + fc167fd commit d1ed176

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

swift/05_array/MyArray.swift

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// Created by Jiandan on 2018/10/10.
3+
// Copyright (c) 2018 Jiandan. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
// <Element> Swift 泛型,此数组支持不同数据类型
9+
public struct MyArray<Element> {
10+
private var data: [Element]
11+
private var capacity = 0 // 数组长度
12+
private var count = 0 // 已保存的数据个数
13+
14+
/// 构造方法
15+
/// - parameter defaultElement: 默认元素,用来占位
16+
/// - parameter capacity: 数组长度
17+
init(defaultElement: Element, capacity: Int) {
18+
data = [Element](repeating: defaultElement, count: capacity)
19+
self.capacity = capacity
20+
}
21+
22+
// 根据 index,查找元素
23+
func find(at index: Int) -> Element? {
24+
// index 必须在 [0, count)
25+
guard index >= 0, index < count else {
26+
return nil
27+
}
28+
29+
return data[index]
30+
}
31+
32+
// 根据 index,删除元素
33+
mutating func delete(at index: Int) -> Bool {
34+
// index 必须在 [0, count)
35+
guard index >= 0, index < count else {
36+
return false
37+
}
38+
39+
// [index, count - 1) 从 index 开始,元素分别向前移动一位
40+
for i in index ..< count - 1 {
41+
data[i] = data[i+1]
42+
}
43+
count -= 1
44+
return true
45+
}
46+
47+
// 根据 index 插入元素
48+
mutating func insert(value: Element, at index: Int) -> Bool {
49+
// index 必须在 [0, count)
50+
guard index >= 0, index < count, count < capacity else {
51+
return false
52+
}
53+
54+
// count - 1 ~ index
55+
for i in (index ... count - 1).reversed() {
56+
data[i + 1] = data[i]
57+
}
58+
59+
data[index] = value
60+
count += 1
61+
return true
62+
}
63+
64+
// 添加元素
65+
mutating func add(value: Element) -> Bool {
66+
guard count < capacity else {
67+
return false
68+
}
69+
data[count] = value
70+
count += 1
71+
return true
72+
}
73+
74+
func printAll() {
75+
print("\(data)")
76+
}
77+
}

swift/09_queue/ArrayQueue.swift

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// Created by Jiandan on 2018/10/11.
3+
// Copyright (c) 2018 Jiandan. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
/// 用数组实现的队列
9+
struct ArrayQueue<T>: Queue {
10+
typealias Element = T
11+
12+
/// 数组
13+
private var items: [Element]
14+
/// 数组最大长度
15+
private var capacity = 0
16+
/// 队头下标
17+
private var head = 0
18+
/// 队尾下标
19+
private var tail = 0
20+
21+
/// 构造方法
22+
/// - parameter defaultElement: 默认元素
23+
/// - parameter capacity: 数组长度
24+
init(defaultElement: Element, capacity: Int) {
25+
self.capacity = capacity
26+
items = [Element](repeating: defaultElement, count: capacity)
27+
}
28+
29+
// MARK: Protocol: Queue
30+
31+
var isEmpty: Bool { return head == tail }
32+
33+
var size: Int { return tail - head }
34+
35+
var peek: Element? { return isEmpty ? nil : items[head] }
36+
37+
// 没有数据搬移的实现,即实现了一个有界序列
38+
// mutating func enqueue(newElement: Element) -> Bool {
39+
// // 整个队列都占满了
40+
// if tail == capacity {
41+
// return false
42+
// }
43+
//
44+
// items[tail] = newElement
45+
// tail += 1
46+
// return true
47+
// }
48+
// 有数据搬移的实现,即实现了一个无界序列
49+
mutating func enqueue(newElement: Element) -> Bool {
50+
// 如果 tail == capacity 表示队列末尾没有空间了
51+
if tail == capacity {
52+
// 整个队列都占满了
53+
if head == 0 { return false }
54+
// 数据搬移
55+
for i in head ..< tail {
56+
items[i - head] = items[i]
57+
}
58+
// 搬移完之后重新更新 head 和 tail
59+
tail -= head
60+
head = 0
61+
}
62+
63+
items[tail] = newElement
64+
tail += 1
65+
return true
66+
}
67+
68+
mutating func dequeue() -> Element? {
69+
if isEmpty {
70+
return nil
71+
}
72+
73+
let item = items[head]
74+
head += 1
75+
return item
76+
}
77+
}
78+
79+
/// 使用2个数组实现无界队列,用到 Swift 中 Array 较多的方法
80+
/// 来源:《iOS 面试之道》(故胤道长,唐巧)
81+
struct ArrayQueue2<T>: Queue {
82+
typealias Element = T
83+
84+
/// 输入数组,主要负责入队
85+
var inArray = [Element]()
86+
/// 输出数组,主要负责出队
87+
var outArray = [Element]()
88+
89+
var isEmpty: Bool { return inArray.isEmpty && outArray.isEmpty }
90+
91+
var size: Int { return inArray.count + outArray.count }
92+
93+
// 当 outArray 为空时,返回 inArray 首个元素,否则返回 outArray 末尾元素
94+
var peek: Element? { return outArray.isEmpty ? inArray.first : outArray.last }
95+
96+
mutating func enqueue(newElement: Element) -> Bool {
97+
// inArray 添加元素
98+
inArray.append(newElement)
99+
return true
100+
}
101+
102+
mutating func dequeue() -> Element? {
103+
if outArray.isEmpty {
104+
// 将 inArray 倒序存入 outArray 中
105+
outArray = inArray.reversed()
106+
// 清空 inArray
107+
inArray.removeAll()
108+
}
109+
// 弹出 outArray 最后一个元素
110+
return outArray.popLast()
111+
}
112+
}

swift/09_queue/CircularQueue.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Created by Jiandan on 2018/10/11.
3+
// Copyright (c) 2018 Jiandan. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
/// 循环队列
9+
struct CircularQueue<T>: Queue {
10+
typealias Element = T
11+
12+
/// 数组
13+
private var items: [Element]
14+
/// 数组最大长度
15+
private var capacity = 0
16+
/// 队头下标
17+
private var head = 0
18+
/// 队尾下标
19+
private var tail = 0
20+
21+
/// 构造方法
22+
/// - parameter defaultElement: 默认元素
23+
/// - parameter capacity: 数组长度
24+
init(defaultElement: Element, capacity: Int) {
25+
self.capacity = capacity
26+
items = [Element](repeating: defaultElement, count: capacity)
27+
}
28+
29+
// MARK: Protocol: Queue
30+
31+
var isEmpty: Bool { return head == tail }
32+
33+
var size: Int {
34+
if tail >= head {
35+
return tail - head
36+
} else {
37+
return (tail + 1) + (capacity - head)
38+
}
39+
}
40+
41+
var peek: Element? { return isEmpty ? nil : items[head] }
42+
43+
mutating func enqueue(newElement: Element) -> Bool {
44+
// 整个队列都占满了
45+
if (tail + 1) % capacity == head {
46+
return false
47+
}
48+
49+
items[tail] = newElement
50+
tail = (tail + 1) % capacity
51+
return true
52+
}
53+
54+
mutating func dequeue() -> Element? {
55+
if isEmpty {
56+
return nil
57+
}
58+
59+
let item = items[head]
60+
head = (head + 1) % capacity
61+
return item
62+
}
63+
}

swift/09_queue/Queue.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by Jiandan on 2018/10/11.
3+
// Copyright (c) 2018 Jiandan. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
protocol Queue {
9+
/// 持有的数据类型
10+
associatedtype Element
11+
/// 是否为空
12+
var isEmpty: Bool { get }
13+
/// 队列大小
14+
var size: Int { get }
15+
/// 返回队列头部元素
16+
var peek: Element? { get }
17+
/// 入队
18+
mutating func enqueue(newElement: Element) -> Bool
19+
/// 出队
20+
mutating func dequeue() -> Element?
21+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Created by Jiandan on 2018/10/11.
3+
// Copyright (c) 2018 Jiandan. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
class Node<T> {
9+
var value: T?
10+
var next: Node?
11+
12+
init(value: T) {
13+
self.value = value
14+
}
15+
}
16+
17+
struct QueueBasedOnLinkedList<T>: Queue {
18+
typealias Element = T
19+
20+
/// 队首
21+
var head: Node<Element>?
22+
/// 队尾
23+
var tail: Node<Element>?
24+
25+
// MARK: Protocol: Queue
26+
27+
var isEmpty: Bool { return head == nil }
28+
29+
var size: Int {
30+
var count = 0
31+
while head?.next != nil {
32+
count += 1
33+
}
34+
return count
35+
}
36+
37+
var peek: Element? { return head?.value }
38+
39+
mutating func enqueue(newElement: Element) -> Bool {
40+
if isEmpty {
41+
// 空队列
42+
let node = Node(value: newElement)
43+
head = node
44+
tail = node
45+
} else {
46+
tail!.next = Node(value: newElement)
47+
tail = tail!.next
48+
}
49+
return true
50+
}
51+
52+
mutating func dequeue() -> Element? {
53+
if isEmpty {
54+
return nil
55+
}
56+
57+
let node = head
58+
head = head!.next
59+
return node?.value
60+
}
61+
}

0 commit comments

Comments
 (0)