【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 ...
随机推荐
- Android利用reative_layout生成梅花界面
XML代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...
- Mac中遇到的Eclipse连接不上mySql的问题
1.首先我们在eclipse中连接数据库的过程中,遇到的问题就是如上图.开始百度Communications link failure 这几个关键字.得到的结果基本上就是基本配置参数wait_time ...
- apt-mirror 校验错误文件处理
apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...
- KVM+VNC 虚拟机远程管理
1.安装kvm grep -E -o 'vmx|svm' /proc/cpuinfo #检查服务器是否支持虚拟化(vmx为interl平台.svm是AMD平台) #安装KVM所需软件包: yum gr ...
- python 基础 2.6 for 循环 和if循环 中break
python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...
- Python 单元测试 之setUP() 和 tearDown()
setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件. tearDown:表示释放资源,它在 ...
- 谁能举个通俗易懂的例子告诉我IAAS,SAAS,PAAS的区别?【转自知乎】
是时候祭出这篇吃货文章了: ———————————————————— ———————————————————— ———————————————————— &amp;amp;amp;lt ...
- mysql系列之6.mysql主从同步
普通文件的数据同步 nfs: 网络文件共享 samba: 共享数据 定时任务或守护进程结合 rsync.scp inotify(sersync)+rsync 触发式实时数据同步 ftp数据同步 ssh ...
- 在linux通过源码编译安装redis详细步骤
1.下载源码包 [root@localhost opt]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz 2.解压缩redis ...
- 【JAVA学习】struts2的action中使用session的方法
尊重版权:http://hi.baidu.com/dillisbest/item/0bdc35c0b477b853ad00efac 在Struts2里,假设须要在Action中使用session.能够 ...