-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoArray.java
More file actions
47 lines (42 loc) · 998 Bytes
/
Copy pathTwoArray.java
File metadata and controls
47 lines (42 loc) · 998 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.wg.offerAlgorithm;
public class TwoArray {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[][] arr= new int[3][4];
int[][] arr = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
// for(int i=0;i<3;i++){
// for(int j=0;j<4;j++){
// arr[i][j]=i+1;
// }
// }
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
System.out.println(find(arr,4,4,5));
}
public static boolean find(int[][]arr,int rows,int columns,int key){
boolean found = false;
if(arr!=null && rows>0&&columns>0){
int row=0;
int column= columns-1;
while(row<rows&&column>=0){
if(arr[row][column]==key){
found =true;
break;
}
else if(arr[row][column]>key){
column--;
}else{
row++;
}
}
}
return found;
}
}