From 4bff17aeba2486e6b95c8abc47d8e0f33e688d30 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Fri, 3 May 2024 09:24:55 +0100 Subject: [PATCH] Swift Implementation for LCCI 10.09 --- lcci/10.09.Sorted Matrix Search/README.md | 31 +++++++++++++++++++ lcci/10.09.Sorted Matrix Search/README_EN.md | 31 +++++++++++++++++++ .../10.09.Sorted Matrix Search/Solution.swift | 28 +++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 lcci/10.09.Sorted Matrix Search/Solution.swift diff --git a/lcci/10.09.Sorted Matrix Search/README.md b/lcci/10.09.Sorted Matrix Search/README.md index 9fd79473e6a4b..64d822f4183e0 100644 --- a/lcci/10.09.Sorted Matrix Search/README.md +++ b/lcci/10.09.Sorted Matrix Search/README.md @@ -177,6 +177,37 @@ public class Solution { } ``` +```swift +class Solution { + func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool { + for row in matrix { + if binarySearch(row, target) { + return true + } + } + return false + } + + private func binarySearch(_ array: [Int], _ target: Int) -> Bool { + var left = 0 + var right = array.count - 1 + + while left <= right { + let mid = left + (right - left) / 2 + if array[mid] == target { + return true + } else if array[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } + } + + return false + } +} +``` + ### 方法二:从左下角或右上角搜索 diff --git a/lcci/10.09.Sorted Matrix Search/README_EN.md b/lcci/10.09.Sorted Matrix Search/README_EN.md index 0653d0c428492..3e7cf5f1e988a 100644 --- a/lcci/10.09.Sorted Matrix Search/README_EN.md +++ b/lcci/10.09.Sorted Matrix Search/README_EN.md @@ -185,6 +185,37 @@ public class Solution { } ``` +```swift +class Solution { + func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool { + for row in matrix { + if binarySearch(row, target) { + return true + } + } + return false + } + + private func binarySearch(_ array: [Int], _ target: Int) -> Bool { + var left = 0 + var right = array.count - 1 + + while left <= right { + let mid = left + (right - left) / 2 + if array[mid] == target { + return true + } else if array[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } + } + + return false + } +} +``` + ### Solution 2: Search from the Bottom Left or Top Right diff --git a/lcci/10.09.Sorted Matrix Search/Solution.swift b/lcci/10.09.Sorted Matrix Search/Solution.swift new file mode 100644 index 0000000000000..69bacaf81a6f4 --- /dev/null +++ b/lcci/10.09.Sorted Matrix Search/Solution.swift @@ -0,0 +1,28 @@ +class Solution { + func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool { + for row in matrix { + if binarySearch(row, target) { + return true + } + } + return false + } + + private func binarySearch(_ array: [Int], _ target: Int) -> Bool { + var left = 0 + var right = array.count - 1 + + while left <= right { + let mid = left + (right - left) / 2 + if array[mid] == target { + return true + } else if array[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } + } + + return false + } +} \ No newline at end of file