[LeetCode]Search a 2D Matrix
来源:互联网 发布:omnigraffle mac 编辑:程序博客网 时间:2024/06/06 10:51
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
.
public class Solution {public boolean searchMatrix(int[][] matrix, int target) {int row = matrix.length;int left = 0;int right = row - 1;int mid = (left + right) / 2;while (left <= right) {mid = (left + right) / 2;if (target == matrix[mid][0]) {return true;} else if (target < matrix[mid][0]) {right = mid-1;} else {left = mid+1;}}if (target < matrix[mid][0]){if(mid == 0){return false;}else{return Arrays.binarySearch(matrix[mid-1], target)>=0;}}return Arrays.binarySearch(matrix[mid], target)>=0;}}
0 0
- [leetcode][Search] Search a 2D Matrix
- LeetCode: Search a 2D Matrix
- LeetCode Search a 2D Matrix
- [Leetcode] Search a 2D Matrix
- LeetCode : Search a 2D Matrix
- [LeetCode] Search a 2D Matrix
- Leetcode 74 Search a 2D Matrix
- leetcode 90: Search a 2D Matrix
- [LeetCode]Search a 2D Matrix
- [Leetcode] Search a 2-D matrix
- [Leetcode]Search a 2D Matrix
- [leetcode]Search a 2D Matrix
- LeetCode-Search a 2D Matrix
- [leetcode] Search a 2D Matrix
- LeetCode - Search a 2D Matrix
- LeetCode:Search a 2D Matrix
- LeetCode 74: Search A 2D Matrix
- leetcode之Search a 2D Matrix
- JAVA中的final关键字
- 记录一下这一个星期的面试经历吧
- 6.3.1 处理计划列表
- 对思归者的建议
- oracle存储过程中使用Ref Cursor强类型游标返回结果集
- [LeetCode]Search a 2D Matrix
- android移动视频开发秘籍
- Dedecms 清空栏目后,新建ID不从1开始的解决方法
- 第三篇:Hibernate开发流程和步骤
- 书单:微信之父张小龙的产品经理书单
- 在Storyboard中使用自定义的segue来实现动画过渡
- Linux系统下安装rz/sz命令及使用说明
- XCode快捷键
- 清理和删除svn信息