forked from Jensenczx/CodeEveryday
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40_searchMatrix.java
More file actions
29 lines (29 loc) · 794 Bytes
/
40_searchMatrix.java
File metadata and controls
29 lines (29 loc) · 794 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
25
26
27
28
29
public class Solution {
/**
* @param matrix: A list of lists of integers
* @param: A number you want to search in the matrix
* @return: An integer indicate the occurrence of target in the given matrix
*/
public int searchMatrix(int[][] matrix, int target) {
// write your code here
if(matrix==null||matrix.length==0||matrix[0].length==0)
return 0;
int nums=0;
int col = matrix[0].length;
int row = matrix.length;
int i=col-1;
int j=0;
while(i>=0&&j<row){
if(target>matrix[j][i])
j++;
else if(target<matrix[j][i])
i--;
else if(target==matrix[j][i]){
i--;
j++;
nums++;
}
}
return nums;
}
}