mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
Review Swift codes (#1150)
* feat(swift): review for chapter_computational_complexity * feat(swift): review for chapter_data_structure * feat(swift): review for chapter_array_and_linkedlist * feat(swift): review for chapter_stack_and_queue * feat(swift): review for chapter_hashing * feat(swift): review for chapter_tree * feat(swift): add codes for heap article * feat(swift): review for chapter_heap * feat(swift): review for chapter_graph * feat(swift): review for chapter_searching * feat(swift): review for chapter_sorting * feat(swift): review for chapter_divide_and_conquer * feat(swift): review for chapter_backtracking * feat(swift): review for chapter_dynamic_programming * feat(swift): review for chapter_greedy * feat(swift): review for utils * feat(swift): update ci tool * feat(swift): trailing closure * feat(swift): array init * feat(swift): map index
This commit is contained in:
@@ -17,7 +17,7 @@ func climbingStairsConstraintDP(n: Int) -> Int {
|
||||
dp[2][1] = 0
|
||||
dp[2][2] = 1
|
||||
// 状态转移:从较小子问题逐步求解较大子问题
|
||||
for i in stride(from: 3, through: n, by: 1) {
|
||||
for i in 3 ... n {
|
||||
dp[i][1] = dp[i - 1][2]
|
||||
dp[i][2] = dp[i - 2][1] + dp[i - 2][2]
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ func climbingStairsDP(n: Int) -> Int {
|
||||
dp[1] = 1
|
||||
dp[2] = 2
|
||||
// 状态转移:从较小子问题逐步求解较大子问题
|
||||
for i in stride(from: 3, through: n, by: 1) {
|
||||
for i in 3 ... n {
|
||||
dp[i] = dp[i - 1] + dp[i - 2]
|
||||
}
|
||||
return dp[n]
|
||||
@@ -28,7 +28,7 @@ func climbingStairsDPComp(n: Int) -> Int {
|
||||
}
|
||||
var a = 1
|
||||
var b = 2
|
||||
for _ in stride(from: 3, through: n, by: 1) {
|
||||
for _ in 3 ... n {
|
||||
(a, b) = (b, a + b)
|
||||
}
|
||||
return b
|
||||
|
||||
@@ -11,12 +11,12 @@ func coinChangeDP(coins: [Int], amt: Int) -> Int {
|
||||
// 初始化 dp 表
|
||||
var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)
|
||||
// 状态转移:首行首列
|
||||
for a in stride(from: 1, through: amt, by: 1) {
|
||||
for a in 1 ... amt {
|
||||
dp[0][a] = MAX
|
||||
}
|
||||
// 状态转移:其余行和列
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for a in stride(from: 1, through: amt, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for a in 1 ... amt {
|
||||
if coins[i - 1] > a {
|
||||
// 若超过目标金额,则不选硬币 i
|
||||
dp[i][a] = dp[i - 1][a]
|
||||
@@ -37,8 +37,8 @@ func coinChangeDPComp(coins: [Int], amt: Int) -> Int {
|
||||
var dp = Array(repeating: MAX, count: amt + 1)
|
||||
dp[0] = 0
|
||||
// 状态转移
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for a in stride(from: 1, through: amt, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for a in 1 ... amt {
|
||||
if coins[i - 1] > a {
|
||||
// 若超过目标金额,则不选硬币 i
|
||||
dp[a] = dp[a]
|
||||
|
||||
@@ -10,12 +10,12 @@ func coinChangeIIDP(coins: [Int], amt: Int) -> Int {
|
||||
// 初始化 dp 表
|
||||
var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)
|
||||
// 初始化首列
|
||||
for i in stride(from: 0, through: n, by: 1) {
|
||||
for i in 0 ... n {
|
||||
dp[i][0] = 1
|
||||
}
|
||||
// 状态转移
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for a in stride(from: 1, through: amt, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for a in 1 ... amt {
|
||||
if coins[i - 1] > a {
|
||||
// 若超过目标金额,则不选硬币 i
|
||||
dp[i][a] = dp[i - 1][a]
|
||||
@@ -35,8 +35,8 @@ func coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {
|
||||
var dp = Array(repeating: 0, count: amt + 1)
|
||||
dp[0] = 1
|
||||
// 状态转移
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for a in stride(from: 1, through: amt, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for a in 1 ... amt {
|
||||
if coins[i - 1] > a {
|
||||
// 若超过目标金额,则不选硬币 i
|
||||
dp[a] = dp[a]
|
||||
|
||||
@@ -67,15 +67,15 @@ func editDistanceDP(s: String, t: String) -> Int {
|
||||
let m = t.utf8CString.count
|
||||
var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)
|
||||
// 状态转移:首行首列
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for i in 1 ... n {
|
||||
dp[i][0] = i
|
||||
}
|
||||
for j in stride(from: 1, through: m, by: 1) {
|
||||
for j in 1 ... m {
|
||||
dp[0][j] = j
|
||||
}
|
||||
// 状态转移:其余行和列
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for j in stride(from: 1, through: m, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for j in 1 ... m {
|
||||
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
|
||||
// 若两字符相等,则直接跳过此两字符
|
||||
dp[i][j] = dp[i - 1][j - 1]
|
||||
@@ -94,16 +94,16 @@ func editDistanceDPComp(s: String, t: String) -> Int {
|
||||
let m = t.utf8CString.count
|
||||
var dp = Array(repeating: 0, count: m + 1)
|
||||
// 状态转移:首行
|
||||
for j in stride(from: 1, through: m, by: 1) {
|
||||
for j in 1 ... m {
|
||||
dp[j] = j
|
||||
}
|
||||
// 状态转移:其余行
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for i in 1 ... n {
|
||||
// 状态转移:首列
|
||||
var leftup = dp[0] // 暂存 dp[i-1, j-1]
|
||||
dp[0] = i
|
||||
// 状态转移:其余列
|
||||
for j in stride(from: 1, through: m, by: 1) {
|
||||
for j in 1 ... m {
|
||||
let temp = dp[j]
|
||||
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
|
||||
// 若两字符相等,则直接跳过此两字符
|
||||
|
||||
@@ -49,8 +49,8 @@ func knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {
|
||||
// 初始化 dp 表
|
||||
var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)
|
||||
// 状态转移
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for c in stride(from: 1, through: cap, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for c in 1 ... cap {
|
||||
if wgt[i - 1] > c {
|
||||
// 若超过背包容量,则不选物品 i
|
||||
dp[i][c] = dp[i - 1][c]
|
||||
@@ -69,9 +69,9 @@ func knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {
|
||||
// 初始化 dp 表
|
||||
var dp = Array(repeating: 0, count: cap + 1)
|
||||
// 状态转移
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for i in 1 ... n {
|
||||
// 倒序遍历
|
||||
for c in stride(from: cap, through: 1, by: -1) {
|
||||
for c in (1 ... cap).reversed() {
|
||||
if wgt[i - 1] <= c {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])
|
||||
|
||||
@@ -16,7 +16,7 @@ func minCostClimbingStairsDP(cost: [Int]) -> Int {
|
||||
dp[1] = cost[1]
|
||||
dp[2] = cost[2]
|
||||
// 状态转移:从较小子问题逐步求解较大子问题
|
||||
for i in stride(from: 3, through: n, by: 1) {
|
||||
for i in 3 ... n {
|
||||
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
|
||||
}
|
||||
return dp[n]
|
||||
@@ -29,7 +29,7 @@ func minCostClimbingStairsDPComp(cost: [Int]) -> Int {
|
||||
return cost[n]
|
||||
}
|
||||
var (a, b) = (cost[1], cost[2])
|
||||
for i in stride(from: 3, through: n, by: 1) {
|
||||
for i in 3 ... n {
|
||||
(a, b) = (b, min(a, b) + cost[i])
|
||||
}
|
||||
return b
|
||||
|
||||
@@ -51,16 +51,16 @@ func minPathSumDP(grid: [[Int]]) -> Int {
|
||||
var dp = Array(repeating: Array(repeating: 0, count: m), count: n)
|
||||
dp[0][0] = grid[0][0]
|
||||
// 状态转移:首行
|
||||
for j in stride(from: 1, to: m, by: 1) {
|
||||
for j in 1 ..< m {
|
||||
dp[0][j] = dp[0][j - 1] + grid[0][j]
|
||||
}
|
||||
// 状态转移:首列
|
||||
for i in stride(from: 1, to: n, by: 1) {
|
||||
for i in 1 ..< n {
|
||||
dp[i][0] = dp[i - 1][0] + grid[i][0]
|
||||
}
|
||||
// 状态转移:其余行和列
|
||||
for i in stride(from: 1, to: n, by: 1) {
|
||||
for j in stride(from: 1, to: m, by: 1) {
|
||||
for i in 1 ..< n {
|
||||
for j in 1 ..< m {
|
||||
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]
|
||||
}
|
||||
}
|
||||
@@ -75,15 +75,15 @@ func minPathSumDPComp(grid: [[Int]]) -> Int {
|
||||
var dp = Array(repeating: 0, count: m)
|
||||
// 状态转移:首行
|
||||
dp[0] = grid[0][0]
|
||||
for j in stride(from: 1, to: m, by: 1) {
|
||||
for j in 1 ..< m {
|
||||
dp[j] = dp[j - 1] + grid[0][j]
|
||||
}
|
||||
// 状态转移:其余行
|
||||
for i in stride(from: 1, to: n, by: 1) {
|
||||
for i in 1 ..< n {
|
||||
// 状态转移:首列
|
||||
dp[0] = dp[0] + grid[i][0]
|
||||
// 状态转移:其余列
|
||||
for j in stride(from: 1, to: m, by: 1) {
|
||||
for j in 1 ..< m {
|
||||
dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ func unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {
|
||||
// 初始化 dp 表
|
||||
var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)
|
||||
// 状态转移
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for c in stride(from: 1, through: cap, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for c in 1 ... cap {
|
||||
if wgt[i - 1] > c {
|
||||
// 若超过背包容量,则不选物品 i
|
||||
dp[i][c] = dp[i - 1][c]
|
||||
@@ -30,8 +30,8 @@ func unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {
|
||||
// 初始化 dp 表
|
||||
var dp = Array(repeating: 0, count: cap + 1)
|
||||
// 状态转移
|
||||
for i in stride(from: 1, through: n, by: 1) {
|
||||
for c in stride(from: 1, through: cap, by: 1) {
|
||||
for i in 1 ... n {
|
||||
for c in 1 ... cap {
|
||||
if wgt[i - 1] > c {
|
||||
// 若超过背包容量,则不选物品 i
|
||||
dp[c] = dp[c]
|
||||
|
||||
Reference in New Issue
Block a user