[Swift]LeetCode74. 搜索二維矩陣 | Search a 2D Matrix

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xzxvvwzu-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:git

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

Example 1:github

Input:
matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 3
Output: true

Example 2:算法

Input:
matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 13
Output: false

編寫一個高效的算法來判斷 m x n 矩陣中,是否存在一個目標值。該矩陣具備以下特性:數組

  • 每行中的整數從左到右按升序排列。
  • 每行的第一個整數大於前一行的最後一個整數。

示例 1:微信

輸入:
matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 3
輸出: true

示例 2:ide

輸入:
matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 13
輸出: false

16ms
 1 class Solution {
 2     func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
 3         guard matrix.count > 0 else {
 4             return false
 5         }
 6         
 7         let rows = matrix.count
 8         let columns = matrix[0].count
 9         
10         var first = 0
11         var last = rows*columns-1
12         
13         while first <= last {
14             let mid = first + (last-first)/2
15             
16             if matrix[mid/columns][mid%columns] == target {
17                 return true
18             }
19             
20             if matrix[mid/columns][mid%columns] > target {
21                 last = mid-1
22             }
23             else {
24                 first = mid+1
25             }
26         }
27         
28         return false
29     }
30 }

16msspa

 1 class Solution {
 2     func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
 3         if matrix.count == 0 || matrix[0].count == 0 {
 4             return false
 5         }
 6 
 7         let n = matrix.count
 8         let m = matrix[0].count
 9         var left = 0
10         var right = m * n - 1
11 
12         while left < right {
13             let middle = left + (right - left - 1) / 2
14             if matrix[middle / m][middle % m] < target {
15                 left = middle + 1
16             } else {
17                 right = middle
18             }
19         }
20 
21         return matrix[right / m][right % m] == target
22     }
23 }

20mscode

 1 class Solution {
 2     func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
 3         if matrix.count == 0 || matrix.first!.count <= 0 {
 4             return false
 5         }
 6         // 邊界判斷
 7         if target < matrix.first!.first! || target > matrix.last!.last! {
 8             return false
 9         }
10         // 由於矩陣的每行都有序, 可將矩陣看做"一個數組"進行二分查找
11         var low = 0
12         var high = matrix.count * matrix.first!.count - 1
13         var mid = 0
14         while low <= high {
15             mid = (low + high) / 2
16             let coords = convertIndex(mid, in: matrix) // index轉換爲座標
17             let value = matrix[coords.row][coords.column]
18             if value == target {
19                 return true
20             } else if target < value {
21                 high = mid - 1
22             } else {
23                 low = mid + 1
24             }
25         }
26         return false
27     }
28     
29     private func convertIndex(_ index: Int, in matix: [[Any]]) -> (row: Int, column: Int) {
30         return (row: index / matix.first!.count,
31                 column: index % matix.first!.count)
32     }
33 }

80mshtm

 1 class Solution {
 2     func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
 3         guard !matrix.isEmpty else { return false }
 4 
 5         let lastRowIndex = matrix.count - 1
 6 
 7         for (index, row) in matrix.enumerated() {
 8             guard !row.isEmpty else { continue }
 9             guard target >= row[0] else { continue }
10 
11             if lastRowIndex != index && target >= matrix[index + 1][0] {
12                 continue
13             }
14 
15             for n in row {
16                 if n == target {
17                     return true
18                 }
19             }
20 
21         }
22 
23         return false
24     }
25 }

100ms

 1 class Solution {
 2     func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
 3         let arr = matrix.flatMap{$0}
 4         return binarySearch(arr, 0, arr.count - 1, target)
 5     }
 6     
 7     func binarySearch(_ arr: [Int], _ start: Int, _ end: Int, _ target: Int) -> Bool {
 8         let mid = (start + end ) / 2
 9         guard start <= end else { return false }
10         if target == arr[mid] { return true}
11         if target < arr[mid] {
12             return binarySearch(arr, start, mid - 1, target)
13         } else {
14             return binarySearch(arr,  mid + 1, end, target)
15         }
16     }
17 }

104ms

 1 class Solution {
 2     func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
 3         let arr = matrix.flatMap{$0}
 4         return binarySearch(arr, 0, arr.count - 1, target)
 5     }
 6     
 7     func divide(_ arr: [[Int]], _ start: Int, _ end:Int, _ target: Int) -> Bool {
 8         guard start <= end else { return false }
 9         if start == end {
10             return binarySearch(arr[start], 0, arr[start].count - 1, target)
11         }
12         
13         let mid = (start + end ) / 2
14         let localArr = arr[mid]
15         let high = localArr[localArr.count - 1]
16         if target == high { return true}
17         if high < target {
18             return divide(arr, mid + 1, end, target)
19         } else {
20              return divide(arr, start, mid, target)
21         }
22     }
23     
24     func binarySearch(_ arr: [Int], _ start: Int, _ end: Int, _ target: Int) -> Bool {
25         let mid = (start + end ) / 2
26         guard start <= end else { return false }
27         if target == arr[mid] { return true}
28         if target < arr[mid] {
29             return binarySearch(arr, start, mid - 1, target)
30         } else {
31             return binarySearch(arr,  mid + 1, end, target)
32         }
33     }
34 }
相關文章
相關標籤/搜索