前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >剑指offer第二版(Java最优解)---二维数组中的查找

剑指offer第二版(Java最优解)---二维数组中的查找

作者头像
葆宁
发布2019-04-18 15:09:54
4200
发布2019-04-18 15:09:54
举报
文章被收录于专栏:FREE SOLOFREE SOLOFREE SOLO

题目

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

思路

查找整数时,如果从左上角开始查找,情况较为复杂,可以转换思路,从右上角开始查找:左边数字比较小,右边数字比较大,容易进行判断。

测试用例

1.要查找的数字在数组中

2.要查找的数字不在数组中

3.数组为空

4.数组不满足大小规则

5.数组每行长度不一致.

/**
 * Created by wuyupku on 2019-04-14 17:16
 *
 * @Beijing CHINA
 */


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

    public class OfferDemo05 {

        /*
         * 判断二维数组matrix中是否含有整数a
         * 返回值为a的下标,{-1,-1}代表不存在
         */
        public int[] find(int[][] matrix, int a) {
            int[] index = { -1, -1 };

            // 判断数组是否正确
            if (matrix == null || matrix.length <= 0) {
                System.out.println("数组无效!");
                return index;
            }
            // 判断数组数字的大小是否符合大小规则
            int columns = matrix[0].length;
            for (int i = 0; i < matrix.length; i++) {
                if (matrix[i].length != columns) {
                    System.out.println("数组列数不一致!");
                    return index;
                }
                for (int j = 0; j < matrix[i].length; j++) {
                    if (i == 0 && j == 0)
                        // matrix[0][0]不比较
                        break;
                    if (i == 0) { // 第一行的数,仅和前一列的数比较
                        if (matrix[i][j] < matrix[i][j - 1]) {
                            System.out.println("数组中数字大小不符合要求!");
                            return index;
                        }
                    } else if (j == 0) { // 第一列的数,仅和前一行的数比较
                        if (matrix[i][j] < matrix[i - 1][j]) {
                            System.out.println("数组中数字大小不符合要求!");
                            return index;
                        }
                    } else if (matrix[i][j] < matrix[i - 1][j] || matrix[i][j] < matrix[i][j - 1]) {
                        // 其余位置的数字,和前一行或前一列的比较
                        System.out.println("数组中数字大小不符合要求!");
                        return index;
                    }
                }
            }
            // 正式查找
            int row = 0; // 行数
            int column = matrix[0].length - 1; // 列数
            while (row <= matrix.length - 1 && column >= 0) {
                if (a == matrix[row][column]) {
                    index[0] = row;
                    index[1] = column;
                    System.out.println("数字" + a + "在二维数组中的下标为:" + index[0] + "," + index[1]); // 注意下标是从0开始的
                    return index;
                } else if (a < matrix[row][column]) {
                    column--;
                } else {
                    row++;
                }
            }
            System.out.println("数组中不含数字:" + a);
            return index;
        }

        // ==================================测试代码==================================

        // 1 2 8 9
        // 2 4 9 12
        // 4 7 10 13
        // 6 8 11 15
        // 要查找的数在数组中
        public void test1() {
            System.out.print("test1:");
            int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
            int[] index = find(matrix, 7);
        }

        // 1 2 8 9
        // 2 4 9 12
        // 4 7 10 13
        // 6 8 11 15
        // 要查找的数不在数组中
        public void test2() {
            System.out.print("test2:");
            int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
            int[] index = find(matrix, 5);
        }

        // 数组为空
        public void test3() {
            System.out.print("test3:");
            int[][] matrix = null;
            int[] index = find(matrix, 7);
        }

        // 1 2 8 9
        // 4 3 9 12
        // 4 7 10 13
        // 6 8 11 15
        // 数组不满足大小规则
        public void test4() {
            System.out.print("test4:");
            int[][] matrix = { { 1, 2, 8, 9 }, { 4, 3, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
            int[] index = find(matrix, 7);
        }

        // 数组每行长度不一致
        public void test5() {
            System.out.print("test5:");
            int[][] matrix = { { 1, 2, 8 }, { 4, 3, 9, 12 }, { 4, 7, 10 }, { 6, 8, 11, 15 } };
            int[] index = find(matrix, 7);
        }

        public static void main(String[] args) {
            OfferDemo05 f = new OfferDemo05();
            f.test1(); // 注意下标是从0开始的
            f.test2();
            f.test3();
            f.test4();
            f.test5();
        }
    }
在这里插入图片描述
在这里插入图片描述

应试精简版

/**
 * Created by wuyupku on 2019-04-14 17:40
 *
 * @Beijing CHINA
 */


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

    public class OfferDemo05 {

        /*
         * 判断二维数组matrix中是否含有整数a
         * 返回值为a的下标,{-1,-1}代表不存在
         */
        public int[] find(int[][] matrix, int a) {
            int[] index = { -1, -1 };
            // 正式查找
            int row = 0; // 行数
            int column = matrix[0].length - 1; // 列数
            while (row <= matrix.length - 1 && column >= 0) {
                if (a == matrix[row][column]) {
                    index[0] = row;
                    index[1] = column;
                    System.out.println("数字" + a + "在二维数组中的下标为:" + index[0] + "," + index[1]); // 注意下标是从0开始的
                    return index;
                } else if (a < matrix[row][column]) {
                    column--;
                } else {
                    row++;
                }
            }
            System.out.println("数组中不含数字:" + a);
            return index;
        }

        // ==================================测试代码==================================



        // 数组每行长度不一致
        public void test() {
            System.out.print("test:");
            int[][] matrix = { { 1, 2, 8 }, { 4, 3, 9, 12 }, { 4, 7, 10 }, { 6, 8, 11, 15 } };
            int[] index = find(matrix, 7);
        }

        public static void main(String[] args) {
            OfferDemo05 f = new OfferDemo05();
            
            f.test();
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年04月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 思路
  • 测试用例
  • 应试精简版
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档