forked from xcv58/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solutions for Search-a-2D-Matrix
- Loading branch information
Showing
3 changed files
with
77 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,32 @@ | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool searchMatrix(vector<vector<int> > &matrix, int target) { | ||
if (matrix.size() == 0) { | ||
return false; | ||
} | ||
int n = matrix[0].size(); | ||
|
||
for (int x, y, i = 0, j = matrix.size() * n, mid; i < j; ) { | ||
mid = (i + j) / 2; | ||
x = mid / n; | ||
y = mid % n; | ||
|
||
if (matrix[x][y] == target) { | ||
return true; | ||
} | ||
if (target < matrix[x][y]) { | ||
j = mid; | ||
} else { | ||
i = mid + 1; | ||
} | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
int main() { | ||
return 0; | ||
} |
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,24 @@ | ||
public class Solution { | ||
public boolean searchMatrix(int[][] matrix, int target) { | ||
if (matrix.length == 0) { | ||
return false; | ||
} | ||
int n = matrix[0].length; | ||
|
||
for (int x, y, i = 0, j = matrix.length * n, mid; i < j; ) { | ||
mid = (i + j) / 2; | ||
x = mid / n; | ||
y = mid % n; | ||
|
||
if (matrix[x][y] == target) { | ||
return true; | ||
} | ||
if (target < matrix[x][y]) { | ||
j = mid; | ||
} else { | ||
i = 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,21 @@ | ||
class Solution: | ||
# @param matrix, a list of lists of integers | ||
# @param target, an integer | ||
# @return a boolean | ||
def searchMatrix(self, matrix, target): | ||
if len(matrix) == 0: | ||
return False | ||
n = len(matrix[0]) | ||
i = 0 | ||
j = n * len(matrix) | ||
while i < j: | ||
mid = (i + j) / 2 | ||
x = mid / n | ||
y = mid % n | ||
if matrix[x][y] == target: | ||
return True | ||
if matrix[x][y] > target: | ||
j = mid | ||
else: | ||
i = mid + 1 | ||
return False |