C++二维数组中的查找(剑指offer面试题4)

下面程序是利用向量编写的二维数组的查找,在main函数中完成数组的输入

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

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using std::vector;

bool FindNumber(vector<vector<int>> &arr, int number)
{
    int Row = arr.size();
    int Col = arr[0].size();
    //从向量矩阵的左下角开始搜索,因为是从上到下,从左到右都是递减的,
    //如果目标数number大于左下角的数字,则向右移j++,如果目标数小于左下角的数字,则向上移i--
    int i = Row - 1;
    int j = 0;
    while (i > 0 && j < Col)
    {
        if (arr[i][j] == number)
        {
            cout << "包含整数"<<arr[i][j] << endl;
            return true;
            //break;
        }
        else if (arr[i][j] < number)
        {
            j++;
        }
        else
        {
            i--;
        }
    }
    cout << "数组中不包含数字" << number << endl;
    return false;
}

int main()
{
    vector<vector<int>> arr;
    //初始化
    int row;
    int column;
    cout << "请输入数组的行数和列数:";
    cin >> row >> column;
    //下面是给向量分配存储空间
    arr.resize(row);
    for (int i = 0; i < row; i++)
    {
        arr[i].resize(column);
    }
    //存入元素
    cout << "请输入数组元素:" << endl;
    for (int m = 0; m < row; m++)
    {
        for (int n = 0; n < column; n++)
        {
            cin >> arr[m][n];
        }
    }
    //查找数字5是否在数组arr中,包含的话,返回true1,不包含的话,返回false0
    cout << FindNumber(arr, 5) << endl;;
    return 0;
}

下面程序是直接利用二维数组编写的二维数组的查找,在Find函数中完成数组的输入

#include <cstdio>
#include <iostream>
using namespace std;

bool Find(int number)
{
    /********************创建数组*************************/
    int rows;
    int columns;
    cout << "请输入二维数组的行数和列数:";
    cin >> rows >> columns;
    int **matrix;
    matrix = new int*[rows];
    for (int i = 0; i < rows; i++)
        matrix[i] = new int[columns];
    //输入二维数组的值  
    cout << "请输入二维数组数据:"<<endl;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cin >> matrix[i][j];
        }
    } 
    //输出二维数组  
    cout << "输出二位数组:" << endl;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            cout << matrix[i][j] << ' ';
        cout << endl;
    }
    /*************判定number是否在二维数组中******************/
    bool found = false;
    if (matrix != nullptr && rows > 0 && columns > 0)
    {
        //从向量矩阵的右上角开始搜索,因为是从上到下,从左到右都是递减的
        //如果目标数number大于右上角的数字,则向右移row++,如果目标数小于右上角的数字,则向上移column--
        int row = 0;
        int column = columns - 1;
        while (row < rows && column >= 0)
        {
            if (matrix[row][column] == number)
            {
                found = true;
                break;
            }
            else if (matrix[row][column]> number)
                column--;
            else
                row++;
        }
    }
    //释放二维数组  
    for (int i = 0; i < rows; i++)
    {
        delete[]matrix[i];
    }
    delete[]matrix;
    cout << "该数组不存在数字" << number << endl;
    return found;
}

int main()
{
    /*cin >> rows;
    cin >> columns;*/
    cout<<Find(0)<<endl;
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_36427732/article/details/79648828