#include<cstdio>

 bool Find(int* matrix, int rows, int columns, int number)
{
bool result = false;
if(matrix != nullptr && rows > && columns > )
{
int row_begin = ;
int col_begin = columns - ; //3 [0, 3] while (columns >= && row_begin < rows)
{
if (matrix[row_begin*columns + col_begin] == number)
{
result = true;
return result;
}
else if (matrix[row_begin*columns + col_begin] > number)
col_begin--; // 2 1
else
row_begin++;
}
}
return result;
} void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected)
{
if (testName != nullptr)
printf("%s begins: ", testName);
bool result = Find(matrix, rows, columns, number);
if (result == expected)
{
printf("passed.\n");
}
else
printf("failed.\n");
} void Test1()
{
int matrix[][] = { {,,,},{,,,},{,,,},{,,,} };
Test("Test1", (int*)matrix, , , , true);
}
void Test2()
{
int matrix[][] = { { ,,, },{ ,,, },{ ,,, },{ ,,, } };
Test("Test1", (int*)matrix, , , , false);
}
void Test3()
{
int matrix[][] = { { ,,, },{ ,,, },{ ,,, },{ ,,, } };
Test("Test1", (int*)matrix, , , , true);
}
void Test4()
{
int matrix[][] = { { ,,, },{ ,,, },{ ,,, },{ ,,, } };
Test("Test1", (int*)matrix, , , , true);
}
void Test5()
{
int matrix[][] = { { ,,, },{ ,,, },{ ,,, },{ ,,, } };
Test("Test1", (int*)matrix, , , , false);
}
void Test6()
{
int matrix[][] = { { ,,, },{ ,,, },{ ,,, },{ ,,, } };
Test("Test1", (int*)matrix, , , , false);
}
void Test7()
{
Test("Test1", nullptr, , , , false);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4(); Test5();
Test6();
Test7();
return ;
}

<offer4> 04_FindInPartiallySortedMatrix的更多相关文章

  1. 剑指offer---4、序列化二叉树

    剑指offer---4.序列化二叉树 一.总结 一句话总结: 1. 对于序列化:使用前序遍历,递归的将二叉树的值转化为字符,并且在每次二叉树的结点不为空时,在转化val所得的字符之后添加一个' , ' ...

  2. 剑指offer--4.重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...

  3. 剑指offer4

    中序遍历(LDR)是二叉树遍历的一种,也叫做中根遍历.中序周游.在二叉树中,先左后根再右.巧记:左根右. 现在有一个问题,已知二叉树的前序遍历和中序遍历:PreOrder:         GDAFE ...

  4. 剑指offer--4.斐波那契数列

    int最大范围(有符号情况下,从第0项0开始)能取到第46项1836311903,47项溢出 时间限制:1秒 空间限制:32768K 热度指数:473928 题目描述 大家都知道斐波那契数列,现在要求 ...

  5. 剑指Offer-4.重建二叉树(C++/Java)

    题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2 ...

  6. 剑指offer4:重建二叉树(后序遍历)

    1. 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4 ...

  7. 剑指offer-4:变态条楼梯

    ##四.变态条楼梯 ###题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. ###分析 也是斐波那契数列问题,根据上述的思路,可 ...

随机推荐

  1. android指纹

    Android 6.0+指纹识别心得 Android 6.0+Fingerprint心得 数据来源:https://blog.csdn.net/lhj1076880929/article/detail ...

  2. MYSQL流程图

    MYSQL流程图

  3. mount –o remount,rw /

    mount –o remount,rw / 重新挂载为已经挂载了的文件系统(以读写权限挂载),需要注意的是,挂载点必须是一个已经存在的目录,这个目录可以不为空.一般用于此目录下的文件为ro权限,需要临 ...

  4. pip第三方模块

    在安装好pip的情况下,在cmd窗口调用命令:pip install package -i --trusted-host http://pypi.douban.com/simple(这是豆瓣的源)

  5. jquery控制css的display属性(显示与隐藏)

    jquery控制div的显示与隐藏,很方便的. 例如: $("#id").show()表示display:block, $("#id").hide()表示dis ...

  6. HashMap(JDK1.9)详解

    一.HashMap的概念. 1.HashMap类的继承实现关系如下:因此HashMap的功能有:可序列化.可克隆等功能. 2.HashMap的数据结构:数组+链表+红黑树. 3.键值对的存储方案:第一 ...

  7. webpack 3 & React 的配置 。

    今天真是难过的一天

  8. UVM中的regmodel建模(一)

    UVM中的regmodel继承自VMM的RAL(Register Abstract Layer),现在可以先将寄存器模型进行XML建模,再通过Synopsys 家的工具ralgen来直接生成regmo ...

  9. Intro to Python for Data Science Learning 5 - Packages

    Packages From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-3-functio ...

  10. pandas练习(三)------ 数据分组

    探索酒类消费数据 相关数据见(github) 步骤1 - 导入pandas库 import pandas as pd 步骤2 - 数据集 path3 = "./data/drinks.csv ...