[LeetCode]Search a 2D Matrix
来源:互联网 发布:excel找出重复数据行 编辑:程序博客网 时间:2024/05/28 06:06
题目:给定一个有序的二维数组,判断数组中是否存在target
算法:逐行进行二分查找
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { for (int i=0; i<matrix.length; ++i) { if (horizontalBinarySearch(matrix, target, matrix[i].length, i)) { return true; } } return false; } /** * Binary Search horizontal * @param matrix * @param target * @param length * @param row * @return */ public boolean horizontalBinarySearch(int[][] matrix, int target, int length, int row) { int mid = 0; int left = 0; int right = length - 1; while (left <= right) { mid = (left + right) / 2; if (target == matrix[row][mid]) { return true; } else if (target < matrix[row][mid]) { right = mid - 1; } else { left = mid + 1; } } return false; }}
2 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
- 文件及文件夹
- 不要做一个浮躁的程序员
- Android 使用 Ant 批量打包
- MVC和MVP
- OpenGL Programming Guide- Red Book 例子程序库 -系列- 1-Introduction to OpenGL-Part1
- [LeetCode]Search a 2D Matrix
- 父类指针向子类指针的强制类型转换
- 编程珠玑中关于二分查找的使用
- Java设计模式之代理模式
- 如何让优秀的程序员更进一步
- 技术网站网站推荐
- 80X86寄存器介绍
- 手机病毒满10年 高风险移动app突破二百万
- 让人心动的jQuery插件和HTML5动画