Search a 2D Matrix
来源:互联网 发布:最新网络词语 编辑:程序博客网 时间:2024/06/05 07:54
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
查找有序二维数组 依次确定其行和列 代码如下:
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int high=matrix.length; int width=matrix[0].length; int left=0; int right=high-1; int midh=0; int midw=0; int flag=0; while(left<right){ midh=(left+right)/2; if(matrix[midh][0]<=target){ if(matrix[midh][0]>=target){ flag=1; break; } else left=midh+1; } else{ right=midh-1; } } if(flag==0)midh=left; left=0; right=width-1; flag=0; while(left<right){ midw=(left+right)/2; if(matrix[midh][midw]==target){ flag=1; break; } if(matrix[midh][midw]>target){ right=midw-1; }else{ left=midw+1; } } if(flag==0)midw=left; if(matrix[midh][midw]==target) return true; else return false; }}
0 0
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D Matrix
- Search a 2D matrix
- Search a 2D matrix
- java class生成jar包
- ArcGIS教程:ArcGIS地质图矢量化技巧概要
- fatal: Not a git repository (or any of the parent directories): .git
- 图的广搜、深搜及拓扑排序
- iOS 资源精选(一)
- Search a 2D Matrix
- 如何在 Mac 上卸载 Java 7 和更高版本
- Oracle 批量建函数 存储过程 脚本
- EhCache 分布式缓存/缓存集群
- 有关ASIHttpRequest arc设置等
- 边记边学PHP-(十二)面向对象编程2
- 浮球阀-上海沪嘉阀门厂
- [人生感悟]实习杂记
- combotree