zoukankan      html  css  js  c++  java
  • 面试题 ——— 二维数组的查找

       



    解法, 能够从右上角或左下角的数開始。 与要寻找的数进行比較。 以左下角为例,假设要查找的数是9,   6小于9, 所以6所在的列都小于9, 删掉该列。 8 小于 9, 同理删掉该列。  11 大于9, 它右边的数肯定大于9, 删掉该行。 同理, 删掉 10 所在的行, 最后 9 等于 9 。找到, 返回true。


    code:

    bool Find(int* arr, int rownum, int colnum, int num)
    {
    	if (arr == NULL || rownum < 0 || colnum<0)
    	{
    		return false;
    	}
    	
    	int row = rownum - 1;
    	int col = 0;
    
    	while (row >= 0 && col <= colnum)
    	{
    		if (arr[row*colnum + col] < num)
    		{
    			col++;
    		}
    		else if(arr[row*colnum + col] > num)
    		{
    			row--;
    		}
    		else
    		{
    			return true;
    		}
    	}
    		return false;	
    }
    
    

     




  • 相关阅读:
    Codeforces 1182E Product Oriented Recurrence 矩阵快速幂
    Codeforces 1182D Complete Mirror hash
    HDU
    HDU
    HDU
    HDU
    HDU
    HDU
    web框架之Vue渐进式框架-安装及入门
    web框架之Jqeury基本使用
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7257396.html
Copyright © 2011-2022 走看看