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:
nuomi1
2024-03-20 21:15:39 +08:00
committed by GitHub
parent 300a781fab
commit 7359a7cb4b
55 changed files with 293 additions and 224 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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