-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSearchEle.java
37 lines (32 loc) · 989 Bytes
/
SearchEle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package matrix;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SearchEle {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int rows = Integer.parseInt(br.readLine());
int cols = Integer.parseInt(br.readLine());
int[][] mat = new int[rows][cols];
for (int i = 0; i < rows; i++) {
String[] temp = br.readLine().trim().split("\\s+");
for (int j = 0; j < cols; j++)
mat[i][j] = Integer.parseInt(temp[j]);
}
int x = Integer.parseInt(br.readLine());
System.out.println(searchMatrix(mat, x));
br.close();
}
private static boolean searchMatrix(int[][] matrix, int target) {
for (int i = 0, j = matrix[0].length - 1; i < matrix.length && j >= 0;) {
if (matrix[i][j] == target)
return true;
if (matrix[i][j] > target)
j--;
else
i++;
}
return false;
}
}