#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. BigDecimal精度与相等比较的坑

    先想一下,创建BigDecimal对象的时候一般是怎么创建的? new一个,传进去值 BigDecimal.valueOf方法,传进去值 作为一个数字类型,经常有的操作是比较大小,有一种情况是比较是否 ...

  2. vhost-user 分析1

    2018-01-24 占个坑,准备下写vhost-user的东西 vhost-user是vhost-kernel又回到用户空间的实现,其基本思想和vhost-kernel很类似,不过之前在内核的部分现 ...

  3. 高并发秒杀系统方案(JSR303参数校验)

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  4. InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法

    InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法 140628  8:10:48 [Note] Plugi ...

  5. centos 基础修改文件权限

    在centos 下 nginx 默认用户是user = apachegroup = apache 所以需要更改文件和文件夹权限时候需要满足apache用户才能进行 常用方式: $ chmod Runt ...

  6. spark的ML和MLLib两个包区别和联系?

    原文链接:https://www.zhihu.com/question/35225203/answer/123986969 1. 技术角度上,面向的数据集类型不一样:ML的API是面向Dataset的 ...

  7. ConcurrentModificationException

    //需求:如何集合中有给定的元素就在集合中在插入一个元素public class ListIteratorDemo2 { public static void main(String[] args) ...

  8. Linux定时任务出现问题时正确的解决步骤

    但凡是提供服务的,都要有本账.软件服务也不例外.无论是Apache,Nginx,还是我们自己搭建的网站,日志是标配.这里的日志就是一本账. 当定时任务出现问题时,正确的处理步骤是: 1,定时任务服务是 ...

  9. 机器学习理论基础学习1——频率派 VS 贝叶斯派

    频率派 贝叶斯派 theta是个未知的常量,X是随机变量, theta是个随机变量,X是随机变量 MLE最大似然估计 MAE最大后验概率 统计机器学习,优化问题 1)建立模型.概率 2)定义损失函数 ...

  10. iis服务器配置

    对应的步骤在文件中上传了rar文件 1 .net framework 4.0 和 framework 4.5 直接安装程序  先安装4.0 再安装4.5 顺序不能颠倒!下载完直接安装 如果有会提示本机 ...