Skip to content
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

feat: add swift implementation to lcci problem: No.17.11 #2783

Merged
merged 1 commit into from
May 10, 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
22 changes: 22 additions & 0 deletions lcci/17.11.Find Closest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ impl Solution {
}
```

```swift
class Solution {
func findClosest(_ words: [String], _ word1: String, _ word2: String) -> Int {
let inf = Int.max / 2
var i = inf
var j = -inf
var ans = inf

for (k, word) in words.enumerated() {
if word == word1 {
i = k
} else if word == word2 {
j = k
}
ans = min(ans, abs(i - j))
}

return ans
}
}
```

<!-- tabs:end -->

### 方法二:哈希表 + 双指针
Expand Down
22 changes: 22 additions & 0 deletions lcci/17.11.Find Closest/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,28 @@ impl Solution {
}
```

```swift
class Solution {
func findClosest(_ words: [String], _ word1: String, _ word2: String) -> Int {
let inf = Int.max / 2
var i = inf
var j = -inf
var ans = inf

for (k, word) in words.enumerated() {
if word == word1 {
i = k
} else if word == word2 {
j = k
}
ans = min(ans, abs(i - j))
}

return ans
}
}
```

<!-- tabs:end -->

### Solution 2: Hash Table + Two Pointers
Expand Down
19 changes: 19 additions & 0 deletions lcci/17.11.Find Closest/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
func findClosest(_ words: [String], _ word1: String, _ word2: String) -> Int {
let inf = Int.max / 2
var i = inf
var j = -inf
var ans = inf

for (k, word) in words.enumerated() {
if word == word1 {
i = k
} else if word == word2 {
j = k
}
ans = min(ans, abs(i - j))
}

return ans
}
}
Loading