zoukankan      html  css  js  c++  java
  • 剑指offer1 二维数组中的查找

    在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺
    序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一
    个二维数组和一个整数,判断数组中是否含有该整数。
     
    
    
    package z_jzoffer.jz1;
    
    /**
     * 在一个二维数组中(每个一维数组的长度相同),
     * 每一行都按照从左到右递增的顺序排序,
     * 每一列都按照从上到下递增的顺序排序。
     * 请完成一个函数,输入这样的一个二维数组和一个整数,
     * 判断数组中是否含有该整数
     *
     *  1  2  3   4
     *  5  6  7   8
     *  9 10  11  12
     *  13 14 15  16
     */
    
    
     //
    public class Solution {
    
        public static void main(String[] args) {
            int[][] array = {{1,2,3,5},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
            Solution s = new Solution();
            boolean find = s.Find(17, array);
            System.out.println(find);
    
        }
    
        public boolean Find(int target, int [][] array) {
           return FindStep(0,0,target, array);
        }
    
        public boolean FindStep(int i,int j,int target, int [][] array) {
            if(i>array.length-1||j>array[0].length-1){
                return false;
            }
            if(target==array[i][j]){
                return true;
            }else if(target<array[i][j]){
                return false;
            }else{
                if(FindStep(i+1,j,target, array)){
                    return true;
                }else{
                    return FindStep(i,j+1,target, array);
                }
            }
        }
    }
  • 相关阅读:
    数据库概论练习题第一章整理(1)
    eclipse启动报错 see the log file的解决办法
    有关myeclipse8.5.0搭建java web项目环境【未整理】
    Pycharm安装及配置
    Anaconda安装及配置环境
    C#操作字符串方法总结
    SQL语句中的换行符
    委托
    基类VS接口
    泛型及其继承和同一性
  • 原文地址:https://www.cnblogs.com/houchen/p/13532635.html
Copyright © 2011-2022 走看看