P38、面试题3:二维数组中的查找

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

  首先选取数组中右上角的数字。如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字,剔除这个数字所在的行。也就是说,如果要查找的数字不在数组的右上角,则每一次都在数组的查找范围中剔除一行或者一列,这样每一步都可以缩小查找的范围,直到找到要查找的数字,或者查找范围为空。

代码实现:

package com.yyq;

/**
 * Created by Administrator on 2015/9/4.
 */
public class FindInPartiallySortedMatrix {
    public static boolean findInMatrix(int matrix[][],int rows,int colums,int number) {
        boolean found = false;
        if (matrix != null && rows > 0 && colums > 0) {
            int row = 0;
            int colum = colums - 1;
            while (row < rows && colum >= 0) {
                if (matrix[row][colum] == number) {
                    found = true;
                    break;
                } else if (number < matrix[row][colum]) {
                    colum--;
                } else {
                    row++;
                }
            }
        }
        return found;
    }
    public static void Test(String testName, int matrix[][], int rows, int columns, int number){
        if(testName != null){
            System.out.println(testName+"=========================");
        }
        boolean result = findInMatrix(matrix, rows, columns, number);
        System.out.println("result = "+result);
    }

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

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

// 要查找的数是数组中最小的数字
    void Test3()
    {
        int matrix[][] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        Test("Test3:要查找的数是数组中最小的数字", matrix, 4, 4, 1);
    }

// 要查找的数是数组中最大的数字
    void Test4()
    {
        int matrix[][] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        Test("Test4:要查找的数是数组中最大的数字", matrix, 4, 4, 15);
    }

// 要查找的数比数组中最小的数字还小
    void Test5()
    {
        int matrix[][] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        Test("Test5: 要查找的数比数组中最小的数字还小",matrix, 4, 4, 0);
    }

// 要查找的数比数组中最大的数字还大
    void Test6()
    {
        int matrix[][] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        Test("Test6:要查找的数比数组中最大的数字还大", matrix, 4, 4, 16);
    }

//传入只有一个数字的数组
    void Test7()
    {
        int matrix[][] = {{1}};
        Test("Test7:传入只有一个数字的数组", matrix, 1, 1, 1);
    }
// 鲁棒性测试,输入空指针
    void Test8()
    {
        Test("Test8:鲁棒性测试,输入空指针", null, 0, 0, 16);
    }

    public static void main(String[] args){
        FindInPartiallySortedMatrix test = new FindInPartiallySortedMatrix();
        test.Test1();
        test.Test2();
        test.Test3();
        test.Test4();
        test.Test5();
        test.Test6();
        test.Test7();
        test.Test8();
    }
}

输出结果:

Test1:要查找的数在数组中=========================

result = true

Test2:要查找的数不在数组中=========================

result = false

Test3:要查找的数是数组中最小的数字=========================

result = true

Test4:要查找的数是数组中最大的数字=========================

result = true

Test5: 要查找的数比数组中最小的数字还小=========================

result = false

Test6:要查找的数比数组中最大的数字还大=========================

result = false

Test7:传入只有一个数字的数组=========================

result = true

Test8:鲁棒性测试,输入空指针=========================

result = false

原文地址:https://www.cnblogs.com/yangyquin/p/4908253.html