Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit efbd9a2

Browse files
refactor 74
1 parent f71782f commit efbd9a2

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ Your ideas/fixes/algorithms are more than welcome!
529529
|77|[Combinations](https://leetcode.com/problems/combinations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_77.java)|O(n^2) ? |O(1)|Medium|Backtracking
530530
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_76.java)|O(n)|O(k)|Hard|Two Pointers
531531
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_75.java)|O(n)|O(1)|Medium| Two Pointers
532-
|74|[Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_74.java)|O(logn)|O(1)|Medium| Binary Search
532+
|74|[Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_74.java)|O(log(m*n))|O(1)|Medium| Binary Search
533533
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_73.java)|O(mn)|O(mn)|Medium|
534534
|72|[Edit Distance](https://leetcode.com/problems/edit-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_72.java)|O(m*n)|O(m+n)|Hard|
535535
|71|[Simplify Path](https://leetcode.com/problems/simplify-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_71.java)|O(n)|O(n)|Medium| Stack
Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
4+
* 74. Search a 2D Matrix
5+
*
6+
* Write an efficient algorithm that searches for a value in an m x n matrix.
7+
* This matrix has the following properties:
58
69
Integers in each row are sorted from left to right.
710
The first integer of each row is greater than the last integer of the previous row.
@@ -21,18 +24,28 @@
2124
public class _74 {
2225

2326
public boolean searchMatrix(int[][] matrix, int target) {
24-
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
25-
int m = matrix.length, n = matrix[0].length;
26-
if (target < matrix[0][0] || target > matrix[m-1][n-1]) return false;
27-
//just treat it as a sorted list
27+
if (matrix == null || matrix.length == 0
28+
|| matrix[0].length == 0
29+
|| matrix[0][0] > target
30+
|| matrix[matrix.length-1][matrix[0].length-1] < target) {
31+
return false;
32+
}
33+
int m = matrix.length;
34+
int n = matrix[0].length;
2835
int left = 0;
29-
int right = m*n - 1;
30-
while (left != right) {
31-
int mid = (left + right - 1) >> 1;
32-
if (matrix[mid/n][mid%n] < target) left = mid + 1;
33-
else right = mid;
36+
int right = m * n - 1 ;
37+
while (left <= right) {
38+
int mid = left + (right-left)/2;
39+
int row = mid/n;
40+
int col = mid%n;
41+
if (matrix[row][col] == target) {
42+
return true;
43+
} else if (matrix[row][col] > target) {
44+
right = mid-1;
45+
} else {
46+
left = mid+1;
47+
}
3448
}
35-
return matrix[right/n][right%n] == target;
49+
return false;
3650
}
37-
3851
}

0 commit comments

Comments
 (0)