#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. LightOj 1265 - Island of Survival(概率)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1265 题目大意:有一个生存游戏,里面t只老虎,d只鹿,还有一个人,每天都要有两个生物碰 ...

  2. Closest Common Ancestors---poj1470(LCA+离线算法)

    题目链接:http://poj.org/problem?id=1470 题意是给出一颗树,q个查询,每个查询都是求出u和v的LCA:    以下是寻找LCA的预处理过程: void LCA(u){ f ...

  3. CSS3实现背景透明,文字不透明

    最近遇到一个需求,要在图片上显示带有半透明背景的文字,效果如下图所示: 看到这个需求之后,第一反应是使用CSS3中的opacity设置元素的透明度. <!DOCTYPE html> < ...

  4. PHP主动断开与浏览器的连接

    以前整理过一篇<关于PHP连接处理中set_time_limit().connection_status()和ignore_user_abort()深入解析>,是解说浏览器client断开 ...

  5. Java-idea-生成for循环

    itar 生成array for代码块 for (int i = 0; i < array.length; i++) { = array[i]; } itco 生成Collection迭代 fo ...

  6. HTML方法

    HTTP 方法:GET 对比 POST 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP ? 超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信. HTTP 的 ...

  7. 十天精通CSS3(4)

    text-overflow 与 word-wrap text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出时 ...

  8. PHP DB 数据库连接类

    近期观看了一节 PHP 消息队列视频,对于讲师WiconWang提供的代码,在此分享一下,希望能对爱学习的小伙伴有所帮助… <?php // 数据库连接类 class DB{ //私有的属性 p ...

  9. Apache-Shiro介绍

    Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大的网络 ...

  10. 安卓备份 To Do(待办事项)的数据库

    真正路径:/data/data/com.mediatek.todos/databases/todos.db 使用过链接的路径:/data/user/0/com.mediatek.todos/datab ...