forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP74_1.java
More file actions
24 lines (22 loc) · 733 Bytes
/
P74_1.java
File metadata and controls
24 lines (22 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package ch18;
public class P74_1 {
public boolean searchMatrix(int[][] matrix, int target) {
int row = 0;
// 첫 행의 맨 뒤로 지정
int col = matrix[0].length - 1;
// 포인터가 행렬내에 있다면 계속 탐색
while (row < matrix.length && col >= 0) {
// 정답을 찾은 경우 true 리턴
if (matrix[row][col] == target) {
return true;
// 타깃보다 크면 왼쪽으로 이동
} else if (matrix[row][col] > target) {
col--;
// 타깃보다 작은 경우이므로 아래로 이동
} else {
row++;
}
}
return false;
}
}