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

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