二维数组中的查找
来源:互联网 发布:js 判断对象是否存在 编辑:程序博客网 时间:2024/06/05 05:09
题目描述
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
public class NewCode1 { //solution1:把每一行看成有序递增的数组,利用二分查找,通过遍历每一行得到答案 public static boolean Find(int [][] array,int target) { for(int i=0;i<array.length;i++){ int low=0; int high=array[i].length-1; while(low<=high){ int mid=(low+high)/2; if(target>array[i][mid]) low=mid+1; else if(target<array[i][mid]) high=mid-1; else return true; } } return false; } //solution2:左下角开始,遇大右移,遇小上移,直到超过边界都没找到,得false。否则得true public static boolean Find2(int [][] array,int target) { int row = array.length; int col = array[0].length; int x = row - 1, y = 0; while(x >=0 && y<=col - 1){ if(target < array[x][y]){ x--; }else if(target > array[x][y]){ y++; }else { return true; } } return false; }}
阅读全文
0 0
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- 二维数组中的查找
- Java BigDecimal开方
- nginx环境调试确认
- JAVA基础:子类、父类继承关系 静态块、非静态块、构造函数初始化顺序性、静态非静态方法覆盖
- css设置单行居中,多行居左/居右
- eclipse内存配置
- 二维数组中的查找
- Git 创建github远程仓库并关联本地项目
- QT收费吗?
- 欢迎使用CSDN-markdown编辑器
- Unique Binary Search Trees II --lintcode
- sleep和wait的区别
- @RequestMapping用法详解
- EventBus简单使用
- 高晓松三问阿里巴巴合伙人_为什么来 为什么留 为什么战