8000 fixed some algorithm bugs · hotcoder/leetcode-1@4c98ae0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c98ae0

Browse files
committed
fixed some algorithm bugs
1 parent 56720e9 commit 4c98ae0

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

algorithms/cpp/search2DMatrix/search2DMatrix.II.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
class Solution {
3535
public:
3636

37-
3837
bool binary_search(vector<int> &v, int target) {
3938
int low = 0;
4039
int high = v.size()-1;
@@ -52,8 +51,9 @@ class Solution {
5251
}
5352

5453
//using binary_search() to search each rows - slow O(n*log(n))
55-
//the run time is around 840ms for all test case
54+
//the run time is around 140ms for all test case
5655
bool searchMatrix01(vector<vector<int>>& matrix, int target) {
56+
if (matrix.size() == 0 || matrix[0].size()==0) return false;
5757
for (int i=0; i<matrix.size(); i++){
5858
if (target < matrix[i][0] ) return false;
5959
if (binary_search(matrix[i], target)) return true;
@@ -63,10 +63,8 @@ class Solution {
6363
}
6464

6565

66-
67-
6866
//start the liner search from top right corner of matrix. - O(m+n)
69-
//the run time is around 340ms
67+
//the run time is around 64ms
7068
bool searchMatrix02(vector<vector<int>>& matrix, int target) {
7169
if (matrix.size() == 0 || matrix[0].size()==0) return false;
7270
int row=0, col = matrix[0].size() - 1;
@@ -82,7 +80,7 @@ class Solution {
8280
return false;
8381
}
8482

85-
//a bit optimization for methed 2 - the run time is 300ms
83+
//a bit optimization for methed 2 - the run time is 68ms
8684
bool searchMatrix021(vector<vector<int>>& matrix, int target) {
8785
if (matrix.size() == 0 || matrix[0].size()==0) return false;
8886
int row=0, col = matrix[0].size() - 1;
@@ -91,7 +89,7 @@ class Solution {
9189
while ( col>=0 && target < matrix[row][col]) {
9290
col--;
9391
}
94-
while(row < matrix.size() && target > matrix[row][col]){
92+
while(col >=0 && row < matrix.size() && target > matrix[row][col]){
9593
row++;
9694
}
9795

@@ -100,7 +98,7 @@ class Solution {
10098
}
10199

102100
//Optimization: using binary search methed to move `low` and `row`
103-
//however, the run time is around 830ms
101+
//However, the run time is 112ms
104102
bool searchMatrix022(vector<vector<int>>& matrix, int target) {
105103
if (matrix.size() == 0 || matrix[0].size()==0) return false;
106104

@@ -116,23 +114,24 @@ class Solution {
116114
int mid = start + (end - start)/2;
117115
if (target == matrix[row][mid]) return true;
118116
if (target > matrix[row][mid]) {
119-
col = start = mid + 1;
117+
start = mid + 1;
120118
}else {
121-
col = end = mid - 1;
119+
end = mid - 1;
122120
}
123121
}
124-
122+
col = end;
125123
}else{
126-
int start=0, end=row;
124+
int start=row, end=matrix.size()-1;
127125
while(start<=end){
128126
int mid = start + (end - start)/2;
129127
if (target == matrix[mid][col]) return true;
130128
if (target > matrix[mid][col]) {
131-
row = start = mid + 1;
129+
start = mid + 1;
132130
}else{
133-
row = end = mid -1;
131+
end = mid -1;
134132
}
135133
}
134+
row = start;
136135
}
137136

138137
}
@@ -141,11 +140,9 @@ class Solution {
141140

142141

143142
bool searchMatrix(vector<vector<int>>& matrix, int target) {
144-
145-
return searchMatrix022(matrix, target); //840ms ??
146-
return searchMatrix021(matrix, target); //320ms
147-
return searchMatrix02(matrix, target); //340ms
148-
149-
return searchMatrix01(matrix, target); // 840ms
143+
return searchMatrix022(matrix, target); //112ms
144+
return searchMatrix021(matrix, target); //68ms
145+
return searchMatrix02(matrix, target); //64ms
146+
return searchMatrix01(matrix, target); //148ms
150147
}
151148
};

0 commit comments

Comments
 (0)
0