Leetcode 074 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.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
思路:先利用每一行的最后一个数与target判断,确定target可能在的行数;再在确定的那一行进行折半查找。
public class S074 {
public boolean searchMatrix(int[][] matrix, int target) {
int i = 0;
for (;i<matrix.length;i++) {
if (matrix[i][matrix[0].length-1]>=target) {
break;
}
}
if (i == matrix.length)
return false;
int l = 0, r = matrix[0].length-1;
//折半查找
while (l <= r) {
if (target == matrix[i][(l+r)/2])
return true;
else if (target > matrix[i][(l+r)/2])
l = (l+r)/2+1;
else
r = (l+r)/2-1;
}
return false;
}
}
Leetcode 074 Search a 2D Matrix的更多相关文章
- Java for LeetCode 074 Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 74. Search a 2D Matrix 解题思路
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- usaco月赛,2017.1总结
T1:跳舞的奶牛 大致题意:一个体积为k的舞台能够同时容纳k只奶牛一起跳舞,他们每头奶牛的跳舞时间不同,如果有一只奶牛跳完了第k+1头奶牛就会立刻上场跳舞,当所有奶牛跳完舞以后我们认为这次表演结束.现 ...
- matcaffe的blob维度顺序
matcaffe是caffe的matlab接口.caffe本身是C++写的,其blob的维度顺序是[N,C,H,W],N表示batchSize,C表示channel数目,H表示feature map的 ...
- dev Gridcontrol单元格值格式化及在模板中调用命令
<dxg:GridColumn> <dxg:GridColumn.EditSettings> ...
- java 打开浏览器 url
public class openBrowers { public static void main(String[] args) { try { //String url = "http: ...
- 从excel读数据到informix的Found a quote for which there is no matching quote错误
我从excel读取数据,然后存储到Informix数据库里.偶尔会发现出现Found a quote for which there is no matching quote这个错误.调试后发现,是因 ...
- Redis 集群解决方案比较
调研比较了三个Redis集群的解决方案: 系统 贡献者 是否官方Redis实现 编程语言 Twemproxy Twitter 是 C Redis Cluster Redis官方 是 C Codis 豌 ...
- Openjudge-NOI题库-Pell数列
题目描述 Description Pell数列a1, a2, a3, ...的定义是这样的,a1 = 1, a2 = 2, ... , an = 2 * an − 1 + an - 2 (n > ...
- centos php nginx 添加到service
1. nginx A. # vi /etc/init.d/nginx B. #!/bin/sh # Comments to support chkconfig on RedHat Linux # ch ...
- view 上推效果
http://www.cocoachina.com/ios/20160307/15586.html
- 在MVC里使用 HttpContext.Response输出内容
public ActionResult About() { byte[] ss = System.Text.Encoding.UTF8.GetBytes("111122"); Ht ...