LintCode: Search A 2d Matrix

简介:

1.

设查找的数位y,第一行最后一列的数位x

如果x<y,x是第一行最大的,所以第一行都小于y,删除第一行;

如果x>y,x是最后一列最小的,所以最后一列都大于y,删除最后一列;

这样保证x永远在可能有解的矩阵的第一行,最后一列。

时间复杂度:O(m+n)

复制代码
 1 class Solution {
 2 public:
 3     /**
 4      * @param matrix, a list of lists of integers
 5      * @param target, an integer
 6      * @return a boolean, indicate whether matrix contains target
 7      */
 8     bool searchMatrix(vector<vector<int> > &matrix, int target) {
 9         // write your code here
10         int m = matrix.size();
11         if (m == 0) {
12             return false;
13         }
14         int n = matrix[0].size();
15         int r = 0, c = n - 1;
16         while (r < m && c >= 0) {
17             if (matrix[r][c] < target) {
18                 r++;
19             } else if (matrix[r][c] > target) {
20                 c--;
21             } else {
22                 return true;
23             }
24         }
25         return false;
26     }
27 };
复制代码

 

2. 分治法1

设查找的数位y,取中心点x,把矩阵分解成4部分

如果x<y,x是A中最大的,所以A都小于y,删除A;

如果x>y,x是D中最小的,所以D都小于y,删除D;

A  | B

————

C  | D

时间复杂度:O(N) = O(N/4)+O(N/2)+O(1), 介于O(N^0.5)~O(N)之间

复制代码
 1 class Solution {
 2 public:
 3     /**
 4      * @param matrix, a list of lists of integers
 5      * @param target, an integer
 6      * @return a boolean, indicate whether matrix contains target
 7      */
 8     bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) {
 9         if (x1 > x2 || y1 > y2) {//empty matrix
10             return false;
11         }
12         int midx = (x1 + x2)>>1;
13         int midy = (y1 + y2)>>1;
14         if (M[midx][midy] == target) {
15             return true;
16         }
17         return (M[midx][midy] > target)?
18         (helper(M, x1, y1, x2, midy-1, target)||helper(M, x1, midy, midx-1, y2, target)):
19         (helper(M, x1, midy+1, x2, y2, target)||helper(M, midx+1, y1, x2, midy, target));
20         
21     }
22     bool searchMatrix(vector<vector<int> > &matrix, int target) {
23         // write your code here
24         int m = matrix.size();
25         if (m == 0) {
26             return false;
27         }
28         int n = matrix[0].size();
29         return helper(matrix, 0, 0, m - 1, n - 1, target);
30     }
31 };
复制代码

 

3. 分治法2 

设查找的数为y,在中线找到这样两个数x1,x2,使得x1<y,x2>y,把矩阵分成4部分

A|     B

————

C|    D

x1是A中最大的,所以A都小于y,删掉A;

x2是D中最小的,所以D都大于y,删掉D;

时间复杂度:O(N)=2O(N/4)+O(logn), 为O(N^0.5) 

复制代码
 1 class Solution {
 2 public:
 3     /**
 4      * @param A, a list of integers
 5      * @param left, an integer
 6      * @param right, an integer
 7      * @param target, an integer
 8      * @return an integer, indicate the index of the last number less than or equal to target in A
 9      */
10     int binarySearch(vector<int> &A, int left, int right, int target) {
11         while (left <= right) {//not an empty list
12             int mid = (left + right) >> 1;
13             if (A[mid] <= target) {
14                 left = mid + 1;//left is in the integer after the last integer less than or equal to target 
15             } else {
16                 right = mid - 1;
17             }
18         }
19         return left - 1;
20     }
21     /**
22      * @param M, a list of lists of integers
23      * @param x1, an integer
24      * @param y1, an integer
25      * @param x2, an integer
26      * @param y2, an integer
27      * @param target, an integer
28      * @return a boolean, indicate whether matrix contains target
29      */
30     bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) {
31         if (x1 > x2 || y1 > y2) {//empty matrix
32             return false;
33         }
34         int midx = (x1 + x2)>>1;
35         int indy = binarySearch(M[midx], y1, y2, target);
36         //M[midx][indy] <= target
37         if ((indy >= y1) && (M[midx][indy] == target)) {
38             return true;
39         }
40         return (helper(M, x1, indy+1, midx-1, y2, target))||(helper(M, midx+1, y1, x2, indy, target));
41         
42     }
43     /**
44      * @param matrix, a list of lists of integers
45      * @param target, an integer
46      * @return a boolean, indicate whether matrix contains target
47      */
48     bool searchMatrix(vector<vector<int> > &matrix, int target) {
49         // write your code here
50         int m = matrix.size();
51         if (m == 0) {
52             return false;
53         }
54         int n = matrix[0].size();
55         return helper(matrix, 0, 0, m - 1, n - 1, target);
56     }
57 };
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5009258.html,如需转载请自行联系原作者

相关文章
|
Python Windows
两个不同python版本的pyinstaller共生 windows
两个不同python版本的pyinstaller共生 windows
331 0
|
消息中间件 缓存 前端开发
COLA架构 入门
COLA架构 入门
4072 0
|
11月前
|
安全 量子技术 云计算
揭秘量子纠缠与量子通信:未来信息技术的革命
揭秘量子纠缠与量子通信:未来信息技术的革命
582 5
EMQ
|
存储 JSON 数据库
MQTTX 1.10.0 发布:CLI高级文件管理与配置
在本次更新中,CLI 版本在文件管理和配置功能方面进行了显著增强。主要更新包括:支持从文件中读取和写入消息、高级配置选项、文本输出模式、以及改进的日志记录。此外,桌面版本现在支持数据库重建,以防止文件损坏引起的问题,并且能更好地处理大数据的展示。这些更新希望为所有 MQTTX 用户提供更加强大和用户友好的体验。
EMQ
856 92
MQTTX 1.10.0 发布:CLI高级文件管理与配置
|
NoSQL Unix Linux
Linux 设备驱动程序(一)(上)
Linux 设备驱动程序(一)
380 62
|
算法
桶排序(简化版)与冒泡排序
桶排序(简化版)与冒泡排序
76 0
|
存储 安全 网络安全
智能家居安全:如何保护你的数字家园
【7月更文挑战第22天】随着技术的进步,智能家居系统已经渗透到我们的日常生活中。这些设备带来了便利,但同时也暴露了我们的数据和隐私于潜在的网络威胁之下。本文将探讨智能家居安全的重要性,并分享一些实用的保护措施,以确保您的数字家园免受黑客的侵害。
|
存储 SQL 监控
OceanBase 的水平扩展与性能优化
【8月更文第31天】随着业务的增长,单一数据库服务器往往难以满足日益增长的数据存储和处理需求。OceanBase 作为一款分布式数据库解决方案,通过其独特的水平扩展能力,能够在不牺牲性能的前提下支持海量数据存储和高并发事务处理。本文将详细介绍 OceanBase 的水平扩展机制,并提供一些性能优化的建议。
1119 0
|
数据采集
【大模型】大语言模型训练数据中的偏差概念及其可能的影响?
【5月更文挑战第5天】【大模型】大语言模型训练数据中的偏差概念及其可能的影响?
|
JSON 开发工具 Android开发
通知消息和透传消息
通知消息和透传消息
1201 0
通知消息和透传消息