Skip to content

Commit fa88687

Browse files
refactor 74
1 parent a476546 commit fa88687

File tree

2 files changed

+38
-45
lines changed

2 files changed

+38
-45
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_74.java

+21-19
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,31 @@
2323
*/
2424
public class _74 {
2525

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
2829
|| matrix[0].length == 0
2930
|| matrix[0][0] > target
3031
|| 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;
4733
}
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;
4851
}
49-
return false;
5052
}
5153
}

Diff for: src/test/java/com/fishercoder/_74Test.java

+17-26
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._74;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static junit.framework.Assert.assertEquals;
98

109
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;
1613

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+
}
2118

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+
}
3829
}

0 commit comments

Comments
 (0)