【Lintcode】028.Search a 2D Matrix
题目:
Write an efficient algorithm that searches for a value in an m x n matrix.
This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
题解:
Solution 1 ()
class Solution {
public:
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if (matrix.empty() || matrix[].empty()) {
return false;
} int rowEnd = matrix.size() - ;
int colEnd = matrix[].size() - ;
int start = , mid = ;
//rowMid
while (start + < rowEnd) {
mid = start + (rowEnd - start) / ;
if (matrix[mid][] == target) {
return true;
} else if (matrix[mid][] > target) {
rowEnd = mid;
} else {
start = mid;
}
} int row = ;
if (matrix[start][] <= target && matrix[start][colEnd] >= target) {
row = start;
} else if (matrix[rowEnd][] <= target && matrix[rowEnd][colEnd] >= target) {
row = rowEnd;
} else {
return false;
}
start = ; //colMid
while (start + < colEnd) {
mid = start + (colEnd - start) / ;
if (matrix[row][mid] == target) {
return true;
} else if (matrix[row][mid] > target) {
colEnd = mid;
} else {
start = mid;
}
} if(matrix[row][start] == target || matrix[row][colEnd] == target) {
return true;
} return false;
}
};
Solution 2 ()
class Solution {
public:
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if (matrix.empty() || matrix[].empty()) {
return false;
} int m = matrix.size(), n = matrix[].size();
int start = ;
int end = m * n - ;
int mid = ; while (start + < end) {
mid = start + (end - start) / ;
int row = mid / n;
int col = mid % n;
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] > target) {
end = mid;
} else {
start = mid;
}
} if (matrix[start / n][start % n] == target) {
return true;
} else if (matrix[end / n][end % n] == target) {
return true;
} else {
return false;
}
}
};
【Lintcode】028.Search a 2D Matrix的更多相关文章
- 【Lintcode】038.Search a 2D Matrix II
题目: Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- 【刷题-LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- 【一天一道LeetCode】#74. Search a 2D Matrix
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【Lintcode】011.Search Range in Binary Search Tree
题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...
随机推荐
- 扩展MongoDB C# Driver的QueryBuilder
扩展MongoDB C# Driver的QueryBuilder 因为不想直接hardcode "ClassA.MemberA.MemberB" 这种字符串 .写了下面几个类,用于 ...
- Linux - 配置SSH免密通信 - “ssh-keygen”的基本用法
目录 1 什么是SSH 2 配置SSH免密登录 2.1 安装必需的软件 2.2 ssh-keygen创建公钥-私钥对 2.3 ssh-copy-id把A的公钥发送给B 2.4 在A服务器上免密登录B服 ...
- Dubbo(一)Dubbo资料
这个资料绝对权威了:http://dubbo.io/user-guide/
- JavaScrip函数与声明表达式
首先我们看下函数的两种命名方式 1.函数声明,声明一个函数 function test1(){ var a=0; console.log(a); //左一些操作... } 执行结果如下 我们看一下,无 ...
- python发布IIS
参考文档 https://segmentfault.com/a/1190000008909201 http://blog.51cto.com/anngle/1922041 https://www.cn ...
- 在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作
在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作. 如果将Response.End()放在try...catch中,catch会捕捉Thread ...
- windows下的常用命令
net start ... 启动某个服务 net stop ... 停止某个服务 net start 查看所有启动的服务 services.msc 打开服务的界面 ipconfig ...
- C#单元测试(转)
C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...
- computed 计算属性
wepyjs - 小程序组件化开发框架 https://tencent.github.io/wepy/document.html#/?id=wepy%e9%a1%b9%e7%9b%ae%e7%9a%8 ...
- Flask:web表单
客户端发送的所有通过POST发出的请求信息都可以通过request.form获取.但是如果我们要生成表单的HTML代码和验证提交的表单数据那么就需要采用另外的方法.Flask-WTF扩展可以把处理we ...