feat(Swift): update min_cost_climbing_stairs_dp and hash_map_open_addressing (#792)

This commit is contained in:
nuomi1 2023-09-24 22:50:09 +08:00 committed by GitHub
parent ff8e7ceec5
commit 72f243eec3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 52 deletions

View File

@ -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)")

View File

@ -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]

View File

@ -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)")
} }
} }
} }