Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add swift implementation to lcp problem: No.63 #3973

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
merged 1 commit into from
Jan 21, 2025
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
74 changes: 74 additions & 0 deletions lcp/LCP 63. 弹珠游戏/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,80 @@ func ballGame(num int, plate []string) (ans [][]int) {
}
```

#### Swift

```swift
class Solution {
private var plate: [String] = []
private var num: Int = 0
private var m: Int = 0
private var n: Int = 0
private let dirs = [0, 1, 0, -1, 0]

func ballGame(_ num: Int, _ plate: [String]) -> [[Int]] {
self.num = num
self.plate = plate
self.m = plate.count
self.n = plate[0].count
var ans: [[Int]] = []

for i in 1..<m - 1 {
if plate[i][0] == "." && check(i, 0, 0) {
ans.append([i, 0])
}
if plate[i][n - 1] == "." && check(i, n - 1, 2) {
ans.append([i, n - 1])
}
}

for j in 1..<n - 1 {
if plate[0][j] == "." && check(0, j, 1) {
ans.append([0, j])
}
if plate[m - 1][j] == "." && check(m - 1, j, 3) {
ans.append([m - 1, j])
}
}

return ans
}

private func check(_ i: Int, _ j: Int, _ d: Int) -> Bool {
var k = num
var i = i, j = j, d = d

while plate[i][j] != "O" {
if k == 0 {
return false
}

if plate[i][j] == "W" {
d = (d + 3) % 4
} else if plate[i][j] == "E" {
d = (d + 1) % 4
}

i += dirs[d]
j += dirs[d + 1]

if i < 0 || i >= m || j < 0 || j >= n {
return false
}

k -= 1
}

return true
}
}

private extension String {
subscript(_ index: Int) -> Character {
return self[self.index(self.startIndex, offsetBy: index)]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
69 changes: 69 additions & 0 deletions lcp/LCP 63. 弹珠游戏/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Solution {
private var plate: [String] = []
private var num: Int = 0
private var m: Int = 0
private var n: Int = 0
private let dirs = [0, 1, 0, -1, 0]

func ballGame(_ num: Int, _ plate: [String]) -> [[Int]] {
self.num = num
self.plate = plate
self.m = plate.count
self.n = plate[0].count
var ans: [[Int]] = []

for i in 1..<m - 1 {
if plate[i][0] == "." && check(i, 0, 0) {
ans.append([i, 0])
}
if plate[i][n - 1] == "." && check(i, n - 1, 2) {
ans.append([i, n - 1])
}
}

for j in 1..<n - 1 {
if plate[0][j] == "." && check(0, j, 1) {
ans.append([0, j])
}
if plate[m - 1][j] == "." && check(m - 1, j, 3) {
ans.append([m - 1, j])
}
}

return ans
}

private func check(_ i: Int, _ j: Int, _ d: Int) -> Bool {
var k = num
var i = i, j = j, d = d

while plate[i][j] != "O" {
if k == 0 {
return false
}

if plate[i][j] == "W" {
d = (d + 3) % 4
} else if plate[i][j] == "E" {
d = (d + 1) % 4
}

i += dirs[d]
j += dirs[d + 1]

if i < 0 || i >= m || j < 0 || j >= n {
return false
}

k -= 1
}

return true
}
}

private extension String {
subscript(_ index: Int) -> Character {
return self[self.index(self.startIndex, offsetBy: index)]
}
}
Loading