Skip to content

Commit 559d1d6

Browse files
committed
0074. Search a 2D Matrix
1 parent 638b468 commit 559d1d6

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed

markdown/0074. Search a 2D Matrix.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### [74\. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Write an efficient algorithm that searches for a value in an _m_ x _n_ matrix. This matrix has the following properties:
7+
8+
* Integers in each row are sorted from left to right.
9+
* The first integer of each row is greater than the last integer of the previous row.
10+
11+
**Example 1:**
12+
13+
```
14+
Input:
15+
matrix = [
16+
[1, 3, 5, 7],
17+
[10, 11, 16, 20],
18+
[23, 30, 34, 50]
19+
]
20+
target = 3
21+
Output: true
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input:
28+
matrix = [
29+
[1, 3, 5, 7],
30+
[10, 11, 16, 20],
31+
[23, 30, 34, 50]
32+
]
33+
target = 13
34+
Output: false
35+
```
36+
37+
38+
#### Solution
39+
40+
Language: **Java**
41+
42+
```java
43+
class Solution {
44+
   public boolean searchMatrix(int[][] matrix, int target) {
45+
       if (matrix == null || matrix.length == 0) {
46+
           return false;
47+
      }
48+
       int rows = matrix.length;
49+
       int width = matrix[0].length;
50+
51+
       if (width == 0) {
52+
           return false;
53+
      }
54+
       int targetRowIndex = -1;
55+
       for (int i = 0; i < rows; i++) {
56+
           if (matrix[i][0] <= target && matrix[i][width - 1] >= target) {
57+
               targetRowIndex = i;
58+
               break;
59+
          }
60+
      }
61+
       if (targetRowIndex < 0) {
62+
           return false;
63+
      }
64+
       int[] targetRow = matrix[targetRowIndex];
65+
66+
       int left = 0;
67+
       int right = width;
68+
       int medium;
69+
       while (left < right) {
70+
           medium = (left + right) / 2;
71+
           if (targetRow[medium] == target) {
72+
               return true;
73+
          } else if (targetRow[medium] > target) {
74+
               right = medium;
75+
          } else {
76+
               left = medium + 1;
77+
          }
78+
      }
79+
       return false;
80+
  }
81+
}
82+
```
83+
![pic](https://gitee.com/jacobchang/PicBed/raw/master/HGYqH5.png)

src/main/java/leetcode/_74_/Main.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode._74_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[][] matrix = {
10+
{
11+
1, 3, 5, 7
12+
},
13+
{
14+
10, 11, 16, 20
15+
},
16+
{
17+
23, 30, 34, 50
18+
}
19+
};
20+
System.out.println(solution.searchMatrix(matrix, 3));
21+
22+
int[][] matrix2 = {
23+
{
24+
1, 3, 5, 7
25+
},
26+
{
27+
10, 11, 16, 20
28+
},
29+
{
30+
23, 30, 34, 50
31+
}
32+
};
33+
System.out.println(solution.searchMatrix(matrix2, 13));
34+
int[][] matrix3 = {
35+
{
36+
1
37+
}
38+
};
39+
System.out.println(solution.searchMatrix(matrix3, 0));
40+
41+
}
42+
}
43+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package leetcode._74_;
2+
3+
class Solution {
4+
public boolean searchMatrix(int[][] matrix, int target) {
5+
if (matrix == null || matrix.length == 0) {
6+
return false;
7+
}
8+
int rows = matrix.length;
9+
int width = matrix[0].length;
10+
11+
if (width == 0) {
12+
return false;
13+
}
14+
int targetRowIndex = -1;
15+
for (int i = 0; i < rows; i++) {
16+
if (matrix[i][0] <= target && matrix[i][width - 1] >= target) {
17+
targetRowIndex = i;
18+
break;
19+
}
20+
}
21+
if (targetRowIndex < 0) {
22+
return false;
23+
}
24+
int[] targetRow = matrix[targetRowIndex];
25+
26+
int left = 0;
27+
int right = width;
28+
int medium;
29+
while (left < right) {
30+
medium = (left + right) / 2;
31+
if (targetRow[medium] == target) {
32+
return true;
33+
} else if (targetRow[medium] > target) {
34+
right = medium;
35+
} else {
36+
left = medium + 1;
37+
}
38+
}
39+
return false;
40+
}
41+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### [74\. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Write an efficient algorithm that searches for a value in an _m_ x _n_ matrix. This matrix has the following properties:
7+
8+
* Integers in each row are sorted from left to right.
9+
* The first integer of each row is greater than the last integer of the previous row.
10+
11+
**Example 1:**
12+
13+
```
14+
Input:
15+
matrix = [
16+
[1, 3, 5, 7],
17+
[10, 11, 16, 20],
18+
[23, 30, 34, 50]
19+
]
20+
target = 3
21+
Output: true
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input:
28+
matrix = [
29+
[1, 3, 5, 7],
30+
[10, 11, 16, 20],
31+
[23, 30, 34, 50]
32+
]
33+
target = 13
34+
Output: false
35+
```
36+
37+
38+
#### Solution
39+
40+
Language: **Java**
41+
42+
```java
43+
class Solution {
44+
   public boolean searchMatrix(int[][] matrix, int target) {
45+
       if (matrix == null || matrix.length == 0) {
46+
           return false;
47+
      }
48+
       int rows = matrix.length;
49+
       int width = matrix[0].length;
50+
51+
       if (width == 0) {
52+
           return false;
53+
      }
54+
       int targetRowIndex = -1;
55+
       for (int i = 0; i < rows; i++) {
56+
           if (matrix[i][0] <= target && matrix[i][width - 1] >= target) {
57+
               targetRowIndex = i;
58+
               break;
59+
          }
60+
      }
61+
       if (targetRowIndex < 0) {
62+
           return false;
63+
      }
64+
       int[] targetRow = matrix[targetRowIndex];
65+
66+
       int left = 0;
67+
       int right = width;
68+
       int medium;
69+
       while (left < right) {
70+
           medium = (left + right) / 2;
71+
           if (targetRow[medium] == target) {
72+
               return true;
73+
          } else if (targetRow[medium] > target) {
74+
               right = medium;
75+
          } else {
76+
               left = medium + 1;
77+
          }
78+
      }
79+
       return false;
80+
  }
81+
}
82+
```
83+
![pic](https://gitee.com/jacobchang/PicBed/raw/master/HGYqH5.png)

0 commit comments

Comments
 (0)