74. Search a 2D Matrix
来源:互联网 发布:php数组做分页 编辑:程序博客网 时间:2024/06/05 17:17
这道题题目要求很简单,说白了就是二维数组的二分查找,代码如下
import java.util.Random;/** * Created by zhangwd4 on 2017/11/23. */public class LeetCode74 { public boolean searchMatrix(int[][] matrix, int target) { if (matrix == null) return false; int row = matrix.length; if (row == 0) return false; int col = matrix[0].length; if (col == 0) return false; int start = 0, end = row * col - 1; while (start <= end) { int mid = (start + end) / 2; int midValue = matrix[mid / col][mid % col]; //这里表示第x行,第y个。 if (midValue == target) { return true; } else if (midValue < target) { start = mid + 1; } else { end = mid - 1; } } return false; } public static void main(String args[]) { int n = 0; int[][] intss = new int[3][3]; for (int i = 0; i < intss.length; i++) { for (int j = 0; j < intss[0].length; j++) { n += new Random().nextInt(5) + 1; intss[i][j] = n; } } for (int i = 0; i < intss.length; i++) { for (int j = 0; j < intss[0].length; j++) { System.out.print(intss[i][j] + "-"); } System.out.println(); } boolean b = new LeetCode74().searchMatrix(intss, 4); System.out.println(b); }}
阅读全文
0 0
- LeetCode 74. Search a 2D Matrix
- [LeetCode]74.Search a 2D Matrix
- LeetCode --- 74. Search a 2D Matrix
- [Leetcode] 74. Search a 2D Matrix
- [leetcode] 74.Search a 2D Matrix
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- Leetcode 74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- 74. Search a 2D Matrix LeetCode
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- leetcode 74. Search a 2D Matrix
- (二)Spring-boot 配置文件加载顺序
- Deep linking 深度链接
- 使用linux 命令行下载oracle官网jdk失败处理方法
- python里使用difflib库的Differ来比较文本
- C语言中Sizeof与Strlen的区别
- 74. Search a 2D Matrix
- 简单的SpringBoot工程搭建
- python中defaultdict方法的使用
- HTML标签marquee实现滚动效果
- UDP 实例
- BZOJ3827: [Poi2014]Around the world
- MapReduce 读取ORC格式文件
- xss安全漏洞分析以及项目实施解决方案
- 7.5