-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from soapyigu/Search
[Search] Add solutions to Search 2D Matrix I and II
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/search-a-2d-matrix/ | ||
* Primary idea: Search col and then binary search row | ||
* | ||
* Time Complexity: O(log(m + n), Space Complexity: O(1) | ||
*/ | ||
|
||
class Search2DMatrix { | ||
func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool { | ||
if matrix.count == 0 || matrix[0].count == 0 { | ||
return false | ||
} | ||
|
||
let rowNum = searchRow(matrix, target) | ||
return searchCol(matrix[rowNum], target) | ||
} | ||
|
||
private func searchRow(_ matrix: [[Int]], _ target: Int) -> Int { | ||
var left = 0, right = matrix.count - 1 | ||
|
||
while left + 1 < right { | ||
let mid = (right - left) / 2 + left | ||
if matrix[mid][0] == target { | ||
return mid | ||
} else if matrix[mid][0] < target { | ||
left = mid | ||
} else { | ||
right = mid | ||
} | ||
} | ||
|
||
return matrix[right][0] <= target ? right : left | ||
} | ||
|
||
private func searchCol(_ nums: [Int], _ target: Int) -> Bool { | ||
var left = 0, right = nums.count - 1 | ||
|
||
while left <= right { | ||
let mid = (right - left) / 2 + left | ||
if nums[mid] == target { | ||
return true | ||
} else if nums[mid] < target { | ||
left = mid + 1 | ||
} else { | ||
right = mid - 1 | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/search-a-2d-matrix-ii/ | ||
* Primary idea: Start from last element at first row, then move downwards or backwards | ||
* | ||
* Time Complexity: O(m + n), Space Complexity: O(1) | ||
*/ | ||
|
||
class Search2DMatrixII { | ||
func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool { | ||
guard matrix.count > 0 else { | ||
return false | ||
} | ||
|
||
var row = 0, col = matrix[0].count - 1 | ||
|
||
while row < matrix.count && col >= 0 { | ||
if matrix[row][col] == target { | ||
return true | ||
} else if matrix[row][col] < target { | ||
row += 1 | ||
} else { | ||
col -= 1 | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |