Merge pull request #50 from JiandanDream/master
[add] Add the version of Swift.
This commit is contained in:
commit
d1ed17641e
77
swift/05_array/MyArray.swift
Normal file
77
swift/05_array/MyArray.swift
Normal file
@ -0,0 +1,77 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/10.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// <Element> Swift 泛型,此数组支持不同数据类型
|
||||
public struct MyArray<Element> {
|
||||
private var data: [Element]
|
||||
private var capacity = 0 // 数组长度
|
||||
private var count = 0 // 已保存的数据个数
|
||||
|
||||
/// 构造方法
|
||||
/// - parameter defaultElement: 默认元素,用来占位
|
||||
/// - parameter capacity: 数组长度
|
||||
init(defaultElement: Element, capacity: Int) {
|
||||
data = [Element](repeating: defaultElement, count: capacity)
|
||||
self.capacity = capacity
|
||||
}
|
||||
|
||||
// 根据 index,查找元素
|
||||
func find(at index: Int) -> Element? {
|
||||
// index 必须在 [0, count)
|
||||
guard index >= 0, index < count else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return data[index]
|
||||
}
|
||||
|
||||
// 根据 index,删除元素
|
||||
mutating func delete(at index: Int) -> Bool {
|
||||
// index 必须在 [0, count)
|
||||
guard index >= 0, index < count else {
|
||||
return false
|
||||
}
|
||||
|
||||
// [index, count - 1) 从 index 开始,元素分别向前移动一位
|
||||
for i in index ..< count - 1 {
|
||||
data[i] = data[i+1]
|
||||
}
|
||||
count -= 1
|
||||
return true
|
||||
}
|
||||
|
||||
// 根据 index 插入元素
|
||||
mutating func insert(value: Element, at index: Int) -> Bool {
|
||||
// index 必须在 [0, count)
|
||||
guard index >= 0, index < count, count < capacity else {
|
||||
return false
|
||||
}
|
||||
|
||||
// count - 1 ~ index
|
||||
for i in (index ... count - 1).reversed() {
|
||||
data[i + 1] = data[i]
|
||||
}
|
||||
|
||||
data[index] = value
|
||||
count += 1
|
||||
return true
|
||||
}
|
||||
|
||||
// 添加元素
|
||||
mutating func add(value: Element) -> Bool {
|
||||
guard count < capacity else {
|
||||
return false
|
||||
}
|
||||
data[count] = value
|
||||
count += 1
|
||||
return true
|
||||
}
|
||||
|
||||
func printAll() {
|
||||
print("\(data)")
|
||||
}
|
||||
}
|
112
swift/09_queue/ArrayQueue.swift
Normal file
112
swift/09_queue/ArrayQueue.swift
Normal file
@ -0,0 +1,112 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/11.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// 用数组实现的队列
|
||||
struct ArrayQueue<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
/// 数组
|
||||
private var items: [Element]
|
||||
/// 数组最大长度
|
||||
private var capacity = 0
|
||||
/// 队头下标
|
||||
private var head = 0
|
||||
/// 队尾下标
|
||||
private var tail = 0
|
||||
|
||||
/// 构造方法
|
||||
/// - parameter defaultElement: 默认元素
|
||||
/// - parameter capacity: 数组长度
|
||||
init(defaultElement: Element, capacity: Int) {
|
||||
self.capacity = capacity
|
||||
items = [Element](repeating: defaultElement, count: capacity)
|
||||
}
|
||||
|
||||
// MARK: Protocol: Queue
|
||||
|
||||
var isEmpty: Bool { return head == tail }
|
||||
|
||||
var size: Int { return tail - head }
|
||||
|
||||
var peek: Element? { return isEmpty ? nil : items[head] }
|
||||
|
||||
// 没有数据搬移的实现,即实现了一个有界序列
|
||||
// mutating func enqueue(newElement: Element) -> Bool {
|
||||
// // 整个队列都占满了
|
||||
// if tail == capacity {
|
||||
// return false
|
||||
// }
|
||||
//
|
||||
// items[tail] = newElement
|
||||
// tail += 1
|
||||
// return true
|
||||
// }
|
||||
// 有数据搬移的实现,即实现了一个无界序列
|
||||
mutating func enqueue(newElement: Element) -> Bool {
|
||||
// 如果 tail == capacity 表示队列末尾没有空间了
|
||||
if tail == capacity {
|
||||
// 整个队列都占满了
|
||||
if head == 0 { return false }
|
||||
// 数据搬移
|
||||
for i in head ..< tail {
|
||||
items[i - head] = items[i]
|
||||
}
|
||||
// 搬移完之后重新更新 head 和 tail
|
||||
tail -= head
|
||||
head = 0
|
||||
}
|
||||
|
||||
items[tail] = newElement
|
||||
tail += 1
|
||||
return true
|
||||
}
|
||||
|
||||
mutating func dequeue() -> Element? {
|
||||
if isEmpty {
|
||||
return nil
|
||||
}
|
||||
|
||||
let item = items[head]
|
||||
head += 1
|
||||
return item
|
||||
}
|
||||
}
|
||||
|
||||
/// 使用2个数组实现无界队列,用到 Swift 中 Array 较多的方法
|
||||
/// 来源:《iOS 面试之道》(故胤道长,唐巧)
|
||||
struct ArrayQueue2<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
/// 输入数组,主要负责入队
|
||||
var inArray = [Element]()
|
||||
/// 输出数组,主要负责出队
|
||||
var outArray = [Element]()
|
||||
|
||||
var isEmpty: Bool { return inArray.isEmpty && outArray.isEmpty }
|
||||
|
||||
var size: Int { return inArray.count + outArray.count }
|
||||
|
||||
// 当 outArray 为空时,返回 inArray 首个元素,否则返回 outArray 末尾元素
|
||||
var peek: Element? { return outArray.isEmpty ? inArray.first : outArray.last }
|
||||
|
||||
mutating func enqueue(newElement: Element) -> Bool {
|
||||
// inArray 添加元素
|
||||
inArray.append(newElement)
|
||||
return true
|
||||
}
|
||||
|
||||
mutating func dequeue() -> Element? {
|
||||
if outArray.isEmpty {
|
||||
// 将 inArray 倒序存入 outArray 中
|
||||
outArray = inArray.reversed()
|
||||
// 清空 inArray
|
||||
inArray.removeAll()
|
||||
}
|
||||
// 弹出 outArray 最后一个元素
|
||||
return outArray.popLast()
|
||||
}
|
||||
}
|
63
swift/09_queue/CircularQueue.swift
Normal file
63
swift/09_queue/CircularQueue.swift
Normal file
@ -0,0 +1,63 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/11.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// 循环队列
|
||||
struct CircularQueue<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
/// 数组
|
||||
private var items: [Element]
|
||||
/// 数组最大长度
|
||||
private var capacity = 0
|
||||
/// 队头下标
|
||||
private var head = 0
|
||||
/// 队尾下标
|
||||
private var tail = 0
|
||||
|
||||
/// 构造方法
|
||||
/// - parameter defaultElement: 默认元素
|
||||
/// - parameter capacity: 数组长度
|
||||
init(defaultElement: Element, capacity: Int) {
|
||||
self.capacity = capacity
|
||||
items = [Element](repeating: defaultElement, count: capacity)
|
||||
}
|
||||
|
||||
// MARK: Protocol: Queue
|
||||
|
||||
var isEmpty: Bool { return head == tail }
|
||||
|
||||
var size: Int {
|
||||
if tail >= head {
|
||||
return tail - head
|
||||
} else {
|
||||
return (tail + 1) + (capacity - head)
|
||||
}
|
||||
}
|
||||
|
||||
var peek: Element? { return isEmpty ? nil : items[head] }
|
||||
|
||||
mutating func enqueue(newElement: Element) -> Bool {
|
||||
// 整个队列都占满了
|
||||
if (tail + 1) % capacity == head {
|
||||
return false
|
||||
}
|
||||
|
||||
items[tail] = newElement
|
||||
tail = (tail + 1) % capacity
|
||||
return true
|
||||
}
|
||||
|
||||
mutating func dequeue() -> Element? {
|
||||
if isEmpty {
|
||||
return nil
|
||||
}
|
||||
|
||||
let item = items[head]
|
||||
head = (head + 1) % capacity
|
||||
return item
|
||||
}
|
||||
}
|
21
swift/09_queue/Queue.swift
Normal file
21
swift/09_queue/Queue.swift
Normal file
@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/11.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol Queue {
|
||||
/// 持有的数据类型
|
||||
associatedtype Element
|
||||
/// 是否为空
|
||||
var isEmpty: Bool { get }
|
||||
/// 队列大小
|
||||
var size: Int { get }
|
||||
/// 返回队列头部元素
|
||||
var peek: Element? { get }
|
||||
/// 入队
|
||||
mutating func enqueue(newElement: Element) -> Bool
|
||||
/// 出队
|
||||
mutating func dequeue() -> Element?
|
||||
}
|
61
swift/09_queue/QueueBasedOnLinkedList.swift
Normal file
61
swift/09_queue/QueueBasedOnLinkedList.swift
Normal file
@ -0,0 +1,61 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/11.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Node<T> {
|
||||
var value: T?
|
||||
var next: Node?
|
||||
|
||||
init(value: T) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
struct QueueBasedOnLinkedList<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
/// 队首
|
||||
var head: Node<Element>?
|
||||
/// 队尾
|
||||
var tail: Node<Element>?
|
||||
|
||||
// MARK: Protocol: Queue
|
||||
|
||||
var isEmpty: Bool { return head == nil }
|
||||
|
||||
var size: Int {
|
||||
var count = 0
|
||||
while head?.next != nil {
|
||||
count += 1
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
var peek: Element? { return head?.value }
|
||||
|
||||
mutating func enqueue(newElement: Element) -> Bool {
|
||||
if isEmpty {
|
||||
// 空队列
|
||||
let node = Node(value: newElement)
|
||||
head = node
|
||||
tail = node
|
||||
} else {
|
||||
tail!.next = Node(value: newElement)
|
||||
tail = tail!.next
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
mutating func dequeue() -> Element? {
|
||||
if isEmpty {
|
||||
return nil
|
||||
}
|
||||
|
||||
let node = head
|
||||
head = head!.next
|
||||
return node?.value
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user