Advertisement
HXXXXJ

240. Search a 2D Matrix II

Apr 13th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.54 KB | None | 0 0
  1.     func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
  2.         guard matrix.count > 0 && matrix[0].count > 0 else {return false}
  3.         var n = matrix.count
  4.         var m = matrix[0].count
  5.         if target < matrix[0][0] || target > matrix[n - 1][m - 1]  { return false}
  6.         var r = 0
  7.         var c = m - 1
  8.         while r < n && c >= 0 {
  9.             if matrix[r][c] == target {return true}
  10.             if matrix[r][c] > target { c -= 1}
  11.             else if matrix[r][c] < target { r += 1}
  12.         }
  13.         return false
  14.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement