码迷,mamicode.com
首页 > 编程语言 > 详细

在二维数组中查找一个数,二维数组是从左到右,从上到下依次递增

时间:2016-02-29 19:47:20      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

public class FindNum {
    public static boolean findANum(int[][] array, int target) {
        int row = array.length;//行数
        int cloumn = array[0].length;//列数
        int i = 0;
        int j = cloumn - 1;
        boolean found = false;
        while(i <= row-1 && j >= 0) {
            if(target < array[i][j]) {
                j--;
            }
            if(target > array[i][j]) {
                i++;
            }
            if(target == array[i][j]) {
                found = true;
                break;
            }
        }
        return found;
    }
    
    public static void main(String[] args) {
        int a[][]  = {{1,2,8,9},{2,4,9,12},{4,7,10,13}};
        System.out.println(a.length);
        System.out.println(a[0].length);
        System.out.println(findANum(a, 7));
        
    }

}

 

在二维数组中查找一个数,二维数组是从左到右,从上到下依次递增

标签:

原文地址:http://www.cnblogs.com/leehfly/p/5228636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!