zoukankan      html  css  js  c++  java
  • 剑指offer第二版-4.二维数组中的查找

    面试题4:二维数组中的查找

    题目要求:
    一个二维数组中,每一行从左到右递增,每一列从上到下递增。输入一个整数,判断数组中是否含有该整数

    /**
     * @since 2019年2月13日 下午5:08:50
     * @author xuchao
     * 二维数组,从左到右递增,从上到下递增,输入一个整数,判断数组中是否含有
     * 
     * 思路:
     * 从最后一行第一列与目标值比较,若大于目标值,则行下标-1;若小于目标值,则列下标+1
     *
     */
    public class P4_FindInPartiallySortedMatrix {
        
        public static boolean findInPartiallySortedMatrix(int[][] data, int target) {
            if(data==null || data.length==0 ||data[0].length==0) {
                return false;
            }
            int colMax = data[0].length - 1;
            int rowCur = data.length - 1, colCur = 0;
            while(true) {
                if (rowCur < 0 || colCur > colMax) {
                    return false;
                }
                if (data[rowCur][colCur] == target) {
                    return true;
                } else if (data[rowCur][colCur] > target) {
                    rowCur--;
                } else {
                    colCur++;
                }
            }
        }
    
        public static void main(String[] args) {
            int[][] data = {
                    { 1, 2, 8, 9 },
                    { 2, 4, 9, 12 },
                    { 4, 7, 10, 13 },
                    { 6, 8, 11, 15 } };
            System.out.println(findInPartiallySortedMatrix(data, 10));
            System.out.println(findInPartiallySortedMatrix(data, 5));
        }
    }
  • 相关阅读:
    hdu 3996
    poj 3189
    poj 2391
    zoj 3165
    【Visual Studio】
    httpwebrequest Winform 上传图片
    [MVC] win7 下 配置 IIS 问题
    win7 下 升级 vs2008
    [Visual Studio 2010] NET 4.0 WinForm无法引用System.Web.dll的解决方法
    [XML] XML
  • 原文地址:https://www.cnblogs.com/chao-zjj/p/10371148.html
Copyright © 2011-2022 走看看