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
3 changed files with 80 additions and 52 deletions

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