#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. SP11469 SUBSET-Balanced Cow Subsets meet-in-the-middle+状压

    正解:折半搜索 解题报告: 传送门! 这题我开始看到的时候贼开心地就把这题的代码直接粘过来辣 然后就T辣,,,仔细思考一下,为什么呢? 因为会枚举到很多相同的状态 举个eg 20 1 1 1 1 1 ...

  2. linux删除指定行&删除行首空格&替换字符

    打印并删除2~1000行 nl /etc/passwd | sed '2,1000d' |more 删除行首空格 sed -i 's/^[][ ]*//g' file 替换分隔符 说明:文件中数据是由 ...

  3. (3.10)mysql基础深入——mysqld 服务器与客户端连接过程 源码分析【待写】

    (3.10)mysql基础深入——mysqld 服务器与客户端连接过程 源码分析[待写]

  4. tf.nn.embedding_lookup函数的用法

    关于np.random.RandomState.np.random.rand.np.random.random.np.random_sample参考https://blog.csdn.net/lanc ...

  5. OpenCV中PCA实现人脸降维

    前言: PCA是大家经常用来减少数据集的维数,同时保留数据集中对方差贡献最大的特征来达到简化数据集的目的.本文通过使用PCA来提取人脸中的特征脸这个例子,来熟悉下在oepncv中怎样使用PCA这个类. ...

  6. 记两个国外CTF的弱pwn

    两道题都来自CSAW CTF 18.PWN学得不够多,如果哪里错了,欢迎留言交流. 第一个题 get_it checksec检查之后,发现栈保护没开,很可能是栈溢出.IDA打开F5看伪源码. int ...

  7. [LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...

  8. [LeetCode] 197. Rising Temperature_Easy tag: SQL

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  9. python 的 json 转换

    python 的 json 转换 本文为原创文章,禁止转载! 本文以 json.dumps()  和 json.loads() 方法进行 Python 数据和 json 格式之间转换,进行讲解 首先比 ...

  10. Summary: Java Inheritance

    In this tutorial we will discuss about the inheritance in Java. The most fundamental element of Java ...