feat(Swift): update min_cost_climbing_stairs_dp and hash_map_open_addressing (#792)
This commit is contained in:
parent
ff8e7ceec5
commit
72f243eec3
@ -16,6 +16,25 @@ func recur(n: Int) -> Int {
|
|||||||
return n + res
|
return n + res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 使用迭代模拟递归 */
|
||||||
|
func forLoopRecur(n: Int) -> Int {
|
||||||
|
// 使用一个显式的栈来模拟系统调用栈
|
||||||
|
var stack: [Int] = []
|
||||||
|
var res = 0
|
||||||
|
// 递:递归调用
|
||||||
|
for i in stride(from: n, to: 0, by: -1) {
|
||||||
|
// 通过“入栈操作”模拟“递”
|
||||||
|
stack.append(i)
|
||||||
|
}
|
||||||
|
// 归:返回结果
|
||||||
|
while !stack.isEmpty {
|
||||||
|
// 通过“出栈操作”模拟“归”
|
||||||
|
res += stack.removeLast()
|
||||||
|
}
|
||||||
|
// res = 1+2+3+...+n
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
/* 尾递归 */
|
/* 尾递归 */
|
||||||
func tailRecur(n: Int, res: Int) -> Int {
|
func tailRecur(n: Int, res: Int) -> Int {
|
||||||
// 终止条件
|
// 终止条件
|
||||||
@ -48,6 +67,9 @@ enum Recursion {
|
|||||||
res = recursion.recur(n: n)
|
res = recursion.recur(n: n)
|
||||||
print("\n递归函数的求和结果 res = \(res)")
|
print("\n递归函数的求和结果 res = \(res)")
|
||||||
|
|
||||||
|
res = recursion.forLoopRecur(n: n)
|
||||||
|
print("\n使用迭代模拟递归求和结果 res = \(res)")
|
||||||
|
|
||||||
res = recursion.tailRecur(n: n, res: 0)
|
res = recursion.tailRecur(n: n, res: 0)
|
||||||
print("\n尾递归函数的求和结果 res = \(res)")
|
print("\n尾递归函数的求和结果 res = \(res)")
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ func minCostClimbingStairsDP(cost: [Int]) -> Int {
|
|||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
var dp = Array(repeating: 0, count: n + 1)
|
var dp = Array(repeating: 0, count: n + 1)
|
||||||
// 初始状态:预设最小子问题的解
|
// 初始状态:预设最小子问题的解
|
||||||
dp[1] = 1
|
dp[1] = cost[1]
|
||||||
dp[2] = 2
|
dp[2] = cost[2]
|
||||||
// 状态转移:从较小子问题逐步求解较大子问题
|
// 状态转移:从较小子问题逐步求解较大子问题
|
||||||
for i in stride(from: 3, through: n, by: 1) {
|
for i in stride(from: 3, through: n, by: 1) {
|
||||||
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
|
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
|
||||||
|
@ -13,7 +13,7 @@ class HashMapOpenAddressing {
|
|||||||
var loadThres: Double // 触发扩容的负载因子阈值
|
var loadThres: Double // 触发扩容的负载因子阈值
|
||||||
var extendRatio: Int // 扩容倍数
|
var extendRatio: Int // 扩容倍数
|
||||||
var buckets: [Pair?] // 桶数组
|
var buckets: [Pair?] // 桶数组
|
||||||
var removed: Pair // 删除标记
|
var TOMBSTONE: Pair // 删除标记
|
||||||
|
|
||||||
/* 构造方法 */
|
/* 构造方法 */
|
||||||
init() {
|
init() {
|
||||||
@ -22,7 +22,7 @@ class HashMapOpenAddressing {
|
|||||||
loadThres = 2.0 / 3.0
|
loadThres = 2.0 / 3.0
|
||||||
extendRatio = 2
|
extendRatio = 2
|
||||||
buckets = Array(repeating: nil, count: capacity)
|
buckets = Array(repeating: nil, count: capacity)
|
||||||
removed = Pair(key: -1, val: "-1")
|
TOMBSTONE = Pair(key: -1, val: "-1")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 哈希函数 */
|
/* 哈希函数 */
|
||||||
@ -35,22 +35,42 @@ class HashMapOpenAddressing {
|
|||||||
Double(size / capacity)
|
Double(size / capacity)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 搜索 key 对应的桶索引 */
|
||||||
|
func findBucket(key: Int) -> Int {
|
||||||
|
var index = hashFunc(key: key)
|
||||||
|
var firstTombstone = -1
|
||||||
|
// 线性探测,当遇到空桶时跳出
|
||||||
|
while buckets[index] != nil {
|
||||||
|
// 若遇到 key ,返回对应桶索引
|
||||||
|
if buckets[index]!.key == key {
|
||||||
|
// 若之前遇到了删除标记,则将键值对移动至该索引
|
||||||
|
if firstTombstone != -1 {
|
||||||
|
buckets[firstTombstone] = buckets[index]
|
||||||
|
buckets[index] = TOMBSTONE
|
||||||
|
return firstTombstone // 返回移动后的桶索引
|
||||||
|
}
|
||||||
|
return index // 返回桶索引
|
||||||
|
}
|
||||||
|
// 记录遇到的首个删除标记
|
||||||
|
if firstTombstone == -1 && buckets[index] == TOMBSTONE {
|
||||||
|
firstTombstone = index
|
||||||
|
}
|
||||||
|
// 计算桶索引,越过尾部返回头部
|
||||||
|
index = (index + 1) % capacity
|
||||||
|
}
|
||||||
|
// 若 key 不存在,则返回添加点的索引
|
||||||
|
return firstTombstone == -1 ? index : firstTombstone
|
||||||
|
}
|
||||||
|
|
||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
func get(key: Int) -> String? {
|
func get(key: Int) -> String? {
|
||||||
let index = hashFunc(key: key)
|
// 搜索 key 对应的桶索引
|
||||||
// 线性探测,从 index 开始向后遍历
|
let index = findBucket(key: key)
|
||||||
for i in stride(from: 0, to: capacity, by: 1) {
|
// 若找到键值对,则返回对应 val
|
||||||
// 计算桶索引,越过尾部返回头部
|
if buckets[index] != nil, buckets[index] != TOMBSTONE {
|
||||||
let j = (index + i) % capacity
|
return buckets[index]!.val
|
||||||
// 若遇到空桶,说明无此 key ,则返回 nil
|
|
||||||
if buckets[j] == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// 若遇到指定 key ,则返回对应 val
|
|
||||||
if buckets[j]?.key == key, buckets[j] != removed {
|
|
||||||
return buckets[j]?.val
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// 若键值对不存在,则返回 null
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,42 +80,26 @@ class HashMapOpenAddressing {
|
|||||||
if loadFactor() > loadThres {
|
if loadFactor() > loadThres {
|
||||||
extend()
|
extend()
|
||||||
}
|
}
|
||||||
let index = hashFunc(key: key)
|
// 搜索 key 对应的桶索引
|
||||||
// 线性探测,从 index 开始向后遍历
|
let index = findBucket(key: key)
|
||||||
for i in stride(from: 0, through: capacity, by: 1) {
|
// 若找到键值对,则覆盖 val 并返回
|
||||||
// 计算桶索引,越过尾部返回头部
|
if buckets[index] != nil, buckets[index] != TOMBSTONE {
|
||||||
let j = (index + i) % capacity
|
buckets[index]!.val = val
|
||||||
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
return
|
||||||
if buckets[j] == nil || buckets[j] == removed {
|
|
||||||
buckets[j] = Pair(key: key, val: val)
|
|
||||||
size += 1
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 若遇到指定 key ,则更新对应 val
|
|
||||||
if buckets[j]?.key == key {
|
|
||||||
buckets[j]?.val = val
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// 若键值对不存在,则添加该键值对
|
||||||
|
buckets[index] = Pair(key: key, val: val)
|
||||||
|
size += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
func remove(key: Int) {
|
func remove(key: Int) {
|
||||||
let index = hashFunc(key: key)
|
// 搜索 key 对应的桶索引
|
||||||
// 线性探测,从 index 开始向后遍历
|
let index = findBucket(key: key)
|
||||||
for i in stride(from: 0, to: capacity, by: 1) {
|
// 若找到键值对,则用删除标记覆盖它
|
||||||
// 计算桶索引,越过尾部返回头部
|
if buckets[index] != nil, buckets[index] != TOMBSTONE {
|
||||||
let j = (index + i) % capacity
|
buckets[index] = TOMBSTONE
|
||||||
// 若遇到空桶,说明无此 key ,则直接返回
|
size -= 1
|
||||||
if buckets[j] == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 若遇到指定 key ,则标记删除并返回
|
|
||||||
if buckets[j]?.key == key {
|
|
||||||
buckets[j] = removed
|
|
||||||
size -= 1
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +113,7 @@ class HashMapOpenAddressing {
|
|||||||
size = 0
|
size = 0
|
||||||
// 将键值对从原哈希表搬运至新哈希表
|
// 将键值对从原哈希表搬运至新哈希表
|
||||||
for pair in bucketsTmp {
|
for pair in bucketsTmp {
|
||||||
if let pair, pair != removed {
|
if let pair, pair != TOMBSTONE {
|
||||||
put(key: pair.key, val: pair.val)
|
put(key: pair.key, val: pair.val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -118,10 +122,12 @@ class HashMapOpenAddressing {
|
|||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
func print() {
|
func print() {
|
||||||
for pair in buckets {
|
for pair in buckets {
|
||||||
if let pair {
|
if pair == nil {
|
||||||
Swift.print("\(pair.key) -> \(pair.val)")
|
|
||||||
} else {
|
|
||||||
Swift.print("null")
|
Swift.print("null")
|
||||||
|
} else if pair == TOMBSTONE {
|
||||||
|
Swift.print("TOMBSTONE")
|
||||||
|
} else {
|
||||||
|
Swift.print("\(pair!.key) -> \(pair!.val)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user