Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add swift implementation to lcci problem: No.08.10 #2696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 1 commit into doocs:main from klever34:swift-impl-lcci-0810
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lcci/08.10.Color Fill/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,33 @@ impl Solution {
}
```

```swift
class Solution {
private var dirs = [-1, 0, 1, 0, -1]
private var image: [[Int]] = []
private var nc: Int = 0
private var oc: Int = 0

func floodFill(_ image: inout [[Int]], _ sr: Int, _ sc: Int, _ newColor: Int) -> [[Int]] {
self.nc = newColor
self.oc = image[sr][sc]
self.image = image
dfs(sr, sc)
return self.image
}

private func dfs(_ i: Int, _ j: Int) {
if i < 0 || i >= image.count || j < 0 || j >= image[0].count || image[i][j] != oc || image[i][j] == nc {
return
}
image[i][j] = nc
for k in 0..<4 {
dfs(i + dirs[k], j + dirs[k + 1])
}
}
}
```

<!-- tabs:end -->

### 方法二:BFS
Expand Down
27 changes: 27 additions & 0 deletions lcci/08.10.Color Fill/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,33 @@ impl Solution {
}
```

```swift
class Solution {
private var dirs = [-1, 0, 1, 0, -1]
private var image: [[Int]] = []
private var nc: Int = 0
private var oc: Int = 0

func floodFill(_ image: inout [[Int]], _ sr: Int, _ sc: Int, _ newColor: Int) -> [[Int]] {
self.nc = newColor
self.oc = image[sr][sc]
self.image = image
dfs(sr, sc)
return self.image
}

private func dfs(_ i: Int, _ j: Int) {
if i < 0 || i >= image.count || j < 0 || j >= image[0].count || image[i][j] != oc || image[i][j] == nc {
return
}
image[i][j] = nc
for k in 0..<4 {
dfs(i + dirs[k], j + dirs[k + 1])
}
}
}
```

<!-- tabs:end -->

### Solution 2: BFS
Expand Down
24 changes: 24 additions & 0 deletions lcci/08.10.Color Fill/Solution.swift
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
private var dirs = [-1, 0, 1, 0, -1]
private var image: [[Int]] = []
private var nc: Int = 0
private var oc: Int = 0

func floodFill(_ image: inout [[Int]], _ sr: Int, _ sc: Int, _ newColor: Int) -> [[Int]] {
self.nc = newColor
self.oc = image[sr][sc]
self.image = image
dfs(sr, sc)
return self.image
}

private func dfs(_ i: Int, _ j: Int) {
if i < 0 || i >= image.count || j < 0 || j >= image[0].count || image[i][j] != oc || image[i][j] == nc {
return
}
image[i][j] = nc
for k in 0..<4 {
dfs(i + dirs[k], j + dirs[k + 1])
}
}
}

AltStyle によって変換されたページ (->オリジナル) /