剑指Offer第一题:二维数组中的查找

剑指Offer第一题:二维数组中的查找

题目描述

在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

示例

在这里插入图片描述

编程思路

采用《剑指Offer》书中的思路:首先选取数组右上角(或者是左下角,同理)的数字,如果该数字等于要查找的数字,则查找过程结束;如果该数字大于要查找的数字,则剔除这个数字所在的列,向左搜寻(缩小范围);如果该数字小于要查找的数字,则剔除这个数字所在的行,向下搜寻。

方法一:暴力实现

public class q_1 {
    public boolean Find(int target, int [][] array) {
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array.length;j++){
                if(array[i][j]==target){
                    System.out.println("数组中存在该整数"+","+"下标为"+i+" "+j);
                    return true;
                }
//           	System.out.println(i+" "+j+" "+array[i][j]);
            }
        }
        System.out.println("数组中不存在该整数");
        return false;
    }
    public static void main(String[] args){
        int target=11;
        int[][] a={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
        q_1 solution=new q_1();
        solution.Find(target,a);
    }
   
}

方法二

public class q_1 {
    public boolean Find(int target, int [][] array) {   
        int rows = array.length;   //二维数组的行长度
        int cols = array[0].length;    //二维数组的列长度
        if(array != null && rows > 0 && cols > 0) { 
            int row = 0;
            int col = cols - 1;
            while(row < rows && col >= 0) {  
                int temp = array[row][col];
                if (temp > target) {  
                    col--;    //待查找的数小于“角值”时,则行不变列-1向左搜寻
                }
                else if (temp < target) {
                    row++;    //待查找的数大于“角值”时,则列不变行+1向下搜寻
                }
                else {
                    return true;
                }
            }
        }
        return false;
    }
    public static void main(String[] args){
    	int target=9;
    	int[][] a={{1,2,3},{4,5,6},{7,8,9}};
    	q_1 solution=new q_1();
    	boolean flag=solution.Find(target,a);
    	if(flag) {
    		System.out.println("已找到");
    	}
    }

	

}

public class q_1 {
    public boolean Find(int target, int [][] array) {   
        int rows = array.length;   //二维数组的行长度
        int cols = array[0].length;    //二维数组的列长度
        if(array != null && rows > 0 && cols > 0) { 
            int row = 0;
            int col = cols - 1;
            while(row < rows && col >= 0) {  
                int temp = array[row][col];
                if (temp > target) {  
                    col--;    //待查找的数小于“角值”时,则行不变列-1向左搜寻
                }
                else if (temp < target) {
                    row++;    //待查找的数大于“角值”时,则列不变行+1向下搜寻
                }
                else {
                    return true;
                }
            }
        }
        return false;
    }
    public static void main(String[] args){
    	int target=9;
    	int[][] a={{1,2,3},{4,5,6},{7,8,9}};
    	q_1 solution=new q_1();
    	boolean flag=solution.Find(target,a);
    	if(flag) {
    		System.out.println("已找到");
    	}
    }

	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值