剑指offer——面试题4:二维数组中的查找

news/2024/7/7 21:36:43
  1 // 面试题4:二维数组中的查找
  2 // 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按
  3 // 照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个
  4 // 整数,判断数组中是否含有该整数。
  5 
  6 #include <cstdio>
  7 
  8 bool Find(int* matrix, int rows, int columns, int number)
  9 {
 10     bool found = false;
 11 
 12     if(matrix != nullptr && rows > 0 && columns > 0)
 13     {
 14         int row = 0;
 15         int column = columns - 1;
 16         while(row < rows && column >=0)
 17         {
 18             if(matrix[row * columns + column] == number)
 19             {
 20                 found = true;
 21                 break;
 22             }
 23             else if(matrix[row * columns + column] > number)
 24                 -- column;
 25             else
 26                 ++ row;
 27         }
 28     }
 29 
 30     return found;
 31 }
 32 
 33 // ====================测试代码====================
 34 void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected)
 35 {
 36     if(testName != nullptr)
 37         printf("%s begins: ", testName);
 38 
 39     bool result = Find(matrix, rows, columns, number);
 40     if(result == expected)
 41         printf("Passed.\n");
 42     else
 43         printf("Failed.\n");
 44 }
 45 
 46 //  1   2   8   9
 47 //  2   4   9   12
 48 //  4   7   10  13
 49 //  6   8   11  15
 50 // 要查找的数在数组中
 51 void Test1()
 52 {
 53     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 54     Test("Test1", (int*)matrix, 4, 4, 7, true);
 55 }
 56 
 57 //  1   2   8   9
 58 //  2   4   9   12
 59 //  4   7   10  13
 60 //  6   8   11  15
 61 // 要查找的数不在数组中
 62 void Test2()
 63 {
 64     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 65     Test("Test2", (int*)matrix, 4, 4, 5, false);
 66 }
 67 
 68 //  1   2   8   9
 69 //  2   4   9   12
 70 //  4   7   10  13
 71 //  6   8   11  15
 72 // 要查找的数是数组中最小的数字
 73 void Test3()
 74 {
 75     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 76     Test("Test3", (int*)matrix, 4, 4, 1, true);
 77 }
 78 
 79 //  1   2   8   9
 80 //  2   4   9   12
 81 //  4   7   10  13
 82 //  6   8   11  15
 83 // 要查找的数是数组中最大的数字
 84 void Test4()
 85 {
 86     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 87     Test("Test4", (int*)matrix, 4, 4, 15, true);
 88 }
 89 
 90 //  1   2   8   9
 91 //  2   4   9   12
 92 //  4   7   10  13
 93 //  6   8   11  15
 94 // 要查找的数比数组中最小的数字还小
 95 void Test5()
 96 {
 97     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 98     Test("Test5", (int*)matrix, 4, 4, 0, false);
 99 }
100 
101 //  1   2   8   9
102 //  2   4   9   12
103 //  4   7   10  13
104 //  6   8   11  15
105 // 要查找的数比数组中最大的数字还大
106 void Test6()
107 {
108     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
109     Test("Test6", (int*)matrix, 4, 4, 16, false);
110 }
111 
112 // 鲁棒性测试,输入空指针
113 void Test7()
114 {
115     Test("Test7", nullptr, 0, 0, 16, false);
116 }
117 
118 int main(int argc, char* argv[])
119 {
120     Test1();
121     Test2();
122     Test3();
123     Test4();
124     Test5();
125     Test6();
126     Test7();
127 
128     return 0;
129 }
View Code

 

转载于:https://www.cnblogs.com/acm-jing/p/10381558.html


http://www.niftyadmin.cn/n/2008614.html

相关文章

常见网络攻击及防御

ARP攻击 前提是需要熟悉网卡的工作模式&#xff0c;网卡的工作模式分为单播、广播、组播等&#xff0c;arp协议发送请求是广播模式&#xff0c;回复是单播模式。概念&#xff1a;通过ip地址查找目标机器mac地址&#xff0c;源机器通过广播方式发送请求包&#xff08;包里包含目…

office365邮箱设置

POP setting Server name: outlook.office365.com Port: 995 Encryption method: SSL SMTP setting Server name: smtp.office365.com Port: 587 Encryption method: TLS IMAP setting Server name: outlook.office365.com Port: 993 Encryption method: SSL

Nuxt.js 数据双向绑定

假定我们有一个需求&#xff0c;一开始通过mounted()将一个字符串渲染在页面上&#xff0c;但是我们经过操作后修改了数据并且需要将得到的结果重新异步渲染到页面中去&#xff0c;而不是跳转刷新页面来重新渲染 首先模板中data()中定义数据&#xff0c;并且要将定义的数据显示…

Spring MVC 文件下载时候 发现IE不支持

Spring MVC 文件下载时候 发现IE不支持RequestMapping("download")public ResponseEntity<byte[]> download(Long fileKey) throws IOException {HttpHeaders headers new HttpHeaders();String fileNamenew String(massMessage.getFileName().getBytes("…

啊,这连字体都要减肥的世道啊

菠萝&#xff1a;“大佬&#xff0c;你这几个数字一定要用这个字体吗&#xff1f;雅黑or苹方不可以吗&#xff1f;” 设计师大佬一脸委屈地看着菠萝&#xff1a;“可是这个字体好看啊。” 表情如下&#xff1a; 哎没办法&#xff0c;这眼神分分钟让人觉得自己在犯罪。那就只能搞…

Gradle 10分钟上手指南_http://www.cnblogs.com/yjmyzz/p/gradle-getting-start.html

Gradle 10分钟上手指南java的源码构建工具&#xff0c;大致经历了 ant -> maven -> gradle 这个过程&#xff0c;每一次进步&#xff0c;都是在解决之前的工具所带来的问题&#xff0c;简单来说&#xff1a;1. ant 功能虽然也很强大&#xff0c;但是过于灵活&#xff0c;…

LeetCode18.四数之和 JavaScript

给定一个包含 n 个整数的数组 nums 和一个目标值 target&#xff0c;判断 nums 中是否存在四个元素 a&#xff0c;b&#xff0c;c 和 d &#xff0c;使得 a b c d 的值与 target 相等&#xff1f;找出所有满足条件且不重复的四元组。 注意&#xff1a;答案中不可以包含重复的…

SpringMVC+myBatis启动报错:Access denied for user '##.##'@'localhost' (using password: YES)

SpringMVCmyBatis启动报错&#xff1a;Access denied for user ##.##localhost (using password: YES)2016年04月14日 15:00:28阅读数&#xff1a;3782网上找了很多解决办法&#xff1a;大致都是说密码填错或者mysql拒绝访问什么的&#xff0c;让修给mysql数据库user用户的bula…