二维数组中的查找

二维数组中的查找

题目描述

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

思路

也没啥说的,我感觉挺简单的,先竖着找,等找到一个值大于target,那说明那个值在上一行的右侧。但防止那些间隔很大的数组,比如:[[1 7 8] [2 5 6]]。所以再加一个挨行找的循环。

python

class Solution:
    def Find(self, target, array):
        row = len(array)
        col = len(array[0])
        for k in range(0,col):
            for i in range(0,row):
                if array[i][k] > target:
                    i = i-1
                    break

            for j in range(k,col):
                if array[i][j] == target:
                    return 1

        return 0

if __name__ == '__main__':
    a = Solution()
    print(a.Find(7,[[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]))

C++

唉,刚说完简单,同样的思路用在C++上牛客显示我:“段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起 。”
于是去卑微找别人的代码参考了下。

#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class Solution {
public:
	bool Find(int target, vector< vector<int> > array) {
		int rows = array.size(); //行
		int cols = array[0].size(); //列
		bool isFind = false;

			int row = 0;
			int col = cols - 1;
			while (row < rows && col >= 0)
			{
				if (array[row][col] == target)
				{
					isFind = true;
					break;
				}
				else if (array[row][col] > target)
				{
					col--;
				}
				else
				{
					row++;
				}
			}

		return isFind;
	}
};

int main() {
	bool Find();
	int a[2][3] = { 1,2,3,7,8,9 };
	vector<vector<int> > a3(2, vector<int>(3));
	for (int i = 0; i < 2; i++) {//初始化
		for (int j = 0; j < 3; j++) {
			a3[i][j] = a[i][j];
		}
	}

	Solution what;
	what.Find(4, a3);
	cout << what.Find(7, a3) << endl;
	return 0;
};

笔记

c++中二维数组与二维向量的长度
对于二维数组:

int mm[3][5] = { { 1,2,3,4,5 },{ 5,6,7,8,9 },{ 9,10,11,12,13 } };
int toatlnums = sizeof(mm) / sizeof(int);  //元素总个数
int cols = sizeof(mm[0]) / sizeof(int);  //行
int raws = totalnums / cols;  //列

对于二维向量:

vector<vector<int>> matrix = { {1,2,3,4,5},{5,6,7,8,9},{9,10,11,12,13} };
int raws = matrix.size(); //行
int cols = matrix[0].size(): //列
int totalnums = raws * cols; //元素总个数

对于向量:

sizeof(matrix) ;
sizeof(matrix[0]);
sizeof(vector<vector<int>>);
sizeof(vector<int>);
sizeof(vector<double>);
// 以上sizeof的结果都是16

C++中创建二维数组的四种方法

使用一维数组模型二维数组

int a0[] = {1,2,3,4,5,6};
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            cout<<a0[i*columns+j]<<" ";//a0[i*columns+j]等价于a0[i][j]
        }
        cout<<endl;
    }

静态二维数组

int a1[2][3] = {1,2,3,4,5,6};

动态二维数组

    //申请空间
    int** a2 = new int*[rows];
    for(int i=0;i<rows;i++)
        a2[i] = new int[columns];
    //释放空间
    for(int i=0;i<rows;i++)
        delete []a2[i];
    delete []a2;

利用vector创建二维数组

    vector<vector<int> > a3(rows,vector<int>(columns));
    for(int i=0;i<rows;i++){//初始化
         for(int j=0;j<columns;j++){
            a3[i][j] = a1[i][j];
         }
    }

参考

C++代码参考链接.
C++中二维数组与二维向量的长度参考链接.
C++中创建二维数组的四种方法参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值