剑指offer面试题3 二维数组中的查找 (java)

注:java主要可以利用字符串的length方法求出长度解决这个问题带来方便

 1 public class FindNum {
 2     public static void main(String[] args) {
 3         int arry[][] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 4         int findnum = 12;
 5         if(find(arry,findnum)){
 6             System.out.println("find");
 7         }
 8         else
 9             System.out.println("Not find");
10         
11     }
12 
13     private static boolean find(int[][] arry, int findnum) {
14         if(arry==null){
15             System.out.println("arry is null!");
16             return false;
17         }
18         boolean findflag = false;//设置寻找标志
19         int rows = arry.length;//获取二维数组总的行数
20         int row  = 0;
21         int colum = arry[0].length-1;//数组的列数
22         while(row<rows&&colum>=0){
23             if(arry[row][colum]==findnum){
24                 findflag = true;
25                 break;
26             }
27             else if(arry[row][colum]>findnum)
28                 colum--;
29             else
30                 row++;
31                 
32         }
33         return findflag;
34         
35     }
36      
37      
38 }

 

posted @ 2015-04-05 20:04  Android茶话会  阅读(150)  评论(0编辑  收藏  举报