File tree 2 files changed +38
-45
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
2 files changed +38
-45
lines changed Original file line number Diff line number Diff line change 23
23
*/
24
24
public class _74 {
25
25
26
- public boolean searchMatrix (int [][] matrix , int target ) {
27
- if (matrix == null || matrix .length == 0
26
+ public static class Solution1 {
27
+ public boolean searchMatrix (int [][] matrix , int target ) {
28
+ if (matrix == null || matrix .length == 0
28
29
|| matrix [0 ].length == 0
29
30
|| matrix [0 ][0 ] > target
30
31
|| matrix [matrix .length - 1 ][matrix [0 ].length - 1 ] < target ) {
31
- return false ;
32
- }
33
- int m = matrix .length ;
34
- int n = matrix [0 ].length ;
35
- int left = 0 ;
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 ;
32
+ return false ;
47
33
}
34
+ int m = matrix .length ;
35
+ int n = matrix [0 ].length ;
36
+ int left = 0 ;
37
+ int right = m * n - 1 ;
38
+ while (left <= right ) {
39
+ int mid = left + (right - left ) / 2 ;
40
+ int row = mid / n ;
41
+ int col = mid % n ;
42
+ if (matrix [row ][col ] == target ) {
43
+ return true ;
44
+ } else if (matrix [row ][col ] > target ) {
45
+ right = mid - 1 ;
46
+ } else {
47
+ left = mid + 1 ;
48
+ }
49
+ }
50
+ return false ;
48
51
}
49
- return false ;
50
52
}
51
53
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder ;
2
2
3
3
import com .fishercoder .solutions ._74 ;
4
- import org .junit .Before ;
5
4
import org .junit .BeforeClass ;
6
5
import org .junit .Test ;
7
6
8
7
import static junit .framework .Assert .assertEquals ;
9
8
10
9
public class _74Test {
11
- private static _74 test ;
12
- private static boolean actual ;
13
- private static boolean expected ;
14
- private static int target ;
15
- private static int [][] matrix ;
10
+ private static _74 .Solution1 solution1 ;
11
+ private static int target ;
12
+ private static int [][] matrix ;
16
13
17
- @ BeforeClass
18
- public static void setup () {
19
- test = new _74 ();
20
- }
14
+ @ BeforeClass
15
+ public static void setup () {
16
+ solution1 = new _74 . Solution1 ();
17
+ }
21
18
22
- @ Before
23
- public void setupForEachTest () {
24
- }
25
-
26
- @ Test
27
- public void test1 () {
28
- target = 3 ;
29
- matrix = new int [][]{
30
- {1 , 3 , 5 , 7 },
31
- {10 , 11 , 16 , 20 },
32
- {23 , 30 , 34 , 50 },
33
- };
34
- expected = true ;
35
- actual = test .searchMatrix (matrix , target );
36
- assertEquals (expected , actual );
37
- }
19
+ @ Test
20
+ public void test1 () {
21
+ target = 3 ;
22
+ matrix = new int [][] {
23
+ {1 , 3 , 5 , 7 },
24
+ {10 , 11 , 16 , 20 },
25
+ {23 , 30 , 34 , 50 },
26
+ };
27
+ assertEquals (true , solution1 .searchMatrix (matrix , target ));
28
+ }
38
29
}
You can’t perform that action at this time.
0 commit comments