二維數組查找問題

題目:在一個二維數組中,每行都按照從左至右遞增的順序排序,每一列都按照從上到下遞增順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有這個整數。這個例子來自劍指offer
最容易想到就是逐個遍歷矩陣,但是沒有用到題幹每一列和每一行都遞增的信息。通過觀察,從矩陣右上角或左下角開始遍歷,這樣能縮短每次遍歷規模。例如,輸入圖中矩陣和整數7,則從右上角開始查找,9大於7,顯然9所在的列不用查找,則9所在行則需要繼續查找,由於9已經比較過,所以刪除9所在這一列。則需要查找數組爲
刪掉9所在的列之後剩餘數組
7在與8比較,由於8大於7,同理,刪掉8所在列。
刪掉8之後剩餘數組
7與2比較,大於2,於是刪掉2所在行。
這裏寫圖片描述
7月4比較,大於4,刪掉4所在行。
這裏寫圖片描述
7月7比較相等,於是返回真,說明要查找的數在矩陣中。

#include<stdio.h>
#include<iostream>
using namespace std;
bool find(int *matrix,int rows,int columns,int num){
    bool found = false;
    if (matrix != NULL && rows >0 && columns > 0){
        int row = 0;
        int column = columns-1;
        while(row < rows && column >= 0){
            if (matrix[row * columns + column] == num){
                found=true;
                break;
            }
            else if(matrix[row * columns + column] > num)
                column--;
            else
                row++;
        }
    }
    return found;
}
int main(){
    int matrix[][4]={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
    int *p=(int *)matrix;
    cout<<find(p,4,4,7)<<endl;
    return 0;

上面代碼是自己寫的,下面這個代碼是摘自<<劍指offer>>,版權歸作者所有,作者寫的代碼相當有參考價值,特別是測試用例的編寫部分,好好學習。

#include <iostream>

// 二維數組matrix中,每一行都從左到右遞增排序,
// 每一列都從上到下遞增排序
bool Find(int* matrix, int rows, int columns, int number)
{
    bool found = false;

    if(matrix != NULL && rows > 0 && columns > 0)
    {
        int row = 0;
        int column = columns - 1;
        while(row < rows && column >=0)
        {
            if(matrix[row * columns + column] == number)
            {
                found = true;
                break;
            }
            else if(matrix[row * columns + column] > number)
                -- column;
            else
                ++ row;
        }
    }

    return found;
}

// ====================測試代碼====================
void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected)
{
    if(testName != NULL)
        printf("%s begins: ", testName);

    bool result = Find(matrix, rows, columns, number);
    if(result == expected)
        printf("Passed.\n");
    else
        printf("Failed.\n");
}

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

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

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

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

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

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

// 魯棒性測試,輸入空指針
void Test7()
{
    Test("Test7", NULL, 0, 0, 16, false);
}

int main()
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章