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
}
/* 使 */
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 {
//
@ -48,6 +67,9 @@ enum Recursion {
res = recursion.recur(n: n)
print("\n递归函数的求和结果 res = \(res)")
res = recursion.forLoopRecur(n: n)
print("\n使用迭代模拟递归求和结果 res = \(res)")
res = recursion.tailRecur(n: n, res: 0)
print("\n尾递归函数的求和结果 res = \(res)")

View file

@ -13,8 +13,8 @@ func minCostClimbingStairsDP(cost: [Int]) -> Int {
// dp
var dp = Array(repeating: 0, count: n + 1)
//
dp[1] = 1
dp[2] = 2
dp[1] = cost[1]
dp[2] = cost[2]
//
for i in stride(from: 3, through: n, by: 1) {
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]

View file

@ -13,7 +13,7 @@ class HashMapOpenAddressing {
var loadThres: Double //
var extendRatio: Int //
var buckets: [Pair?] //
var removed: Pair //
var TOMBSTONE: Pair //
/* */
init() {
@ -22,7 +22,7 @@ class HashMapOpenAddressing {
loadThres = 2.0 / 3.0
extendRatio = 2
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)
}
/* 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? {
let index = hashFunc(key: key)
// 线 index
for i in stride(from: 0, to: capacity, by: 1) {
//
let j = (index + i) % capacity
// key nil
if buckets[j] == nil {
return nil
}
// key val
if buckets[j]?.key == key, buckets[j] != removed {
return buckets[j]?.val
}
// key
let index = findBucket(key: key)
// val
if buckets[index] != nil, buckets[index] != TOMBSTONE {
return buckets[index]!.val
}
// null
return nil
}
@ -60,42 +80,26 @@ class HashMapOpenAddressing {
if loadFactor() > loadThres {
extend()
}
let index = hashFunc(key: key)
// 线 index
for i in stride(from: 0, through: capacity, by: 1) {
//
let j = (index + i) % capacity
//
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
}
// key
let index = findBucket(key: key)
// val
if buckets[index] != nil, buckets[index] != TOMBSTONE {
buckets[index]!.val = val
return
}
//
buckets[index] = Pair(key: key, val: val)
size += 1
}
/* */
func remove(key: Int) {
let index = hashFunc(key: key)
// 线 index
for i in stride(from: 0, to: capacity, by: 1) {
//
let j = (index + i) % capacity
// key
if buckets[j] == nil {
return
}
// key
if buckets[j]?.key == key {
buckets[j] = removed
size -= 1
return
}
// key
let index = findBucket(key: key)
//
if buckets[index] != nil, buckets[index] != TOMBSTONE {
buckets[index] = TOMBSTONE
size -= 1
}
}
@ -109,7 +113,7 @@ class HashMapOpenAddressing {
size = 0
//
for pair in bucketsTmp {
if let pair, pair != removed {
if let pair, pair != TOMBSTONE {
put(key: pair.key, val: pair.val)
}
}
@ -118,10 +122,12 @@ class HashMapOpenAddressing {
/* */
func print() {
for pair in buckets {
if let pair {
Swift.print("\(pair.key) -> \(pair.val)")
} else {
if pair == nil {
Swift.print("null")
} else if pair == TOMBSTONE {
Swift.print("TOMBSTONE")
} else {
Swift.print("\(pair!.key) -> \(pair!.val)")
}
}
}