Leetcode 74. Search a 2D Matrix & 240. Search a 2D Matrix II
来源:互联网 发布:android aop 编程 编辑:程序博客网 时间:2024/06/15 00:27
74. Search a 2D Matrix
Total Accepted: 92869 Total Submissions: 265460 Difficulty: MediumWrite 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]]
思路:
找行的时候是用的for loop,确定行了之后调用二分法查找。(解法1)
网上也有说法是把这个mn矩阵看成一行,那么共有mn个数,假设是第x个数 ( 0~mn-1),那么对应的row: x/4, 对应的列数:x%4。 (解法2)
或者直接bs找行,然后再bs本行。(解法3)
public class Solution { public boolean searchMatrix(int[][] m, int target){ if (m == null || m.length == 0) return false; int row = m.length-1; int col = m[0].length-1; for (int i = 0 ; i <= row; i++){ if (m[i][0] <= target && m[i][col] >= target) return helper(m, 0, col, i, target); } return false; } boolean helper(int[][] m, int start, int end, int row, int target){ if (start > end) return false; int mid = (start + end)/2; if (m[row][mid] == target){ return true; }else if (m[row][mid] > target){ return helper(m, 0, mid-1, row, target); }else{ return helper(m, mid+1, end, row, target); } }}
240. Search a 2D Matrix II
Total Accepted: 48936 Total Submissions: 134289 Difficulty: MediumWrite 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 in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]]
Given target = 5
, return true
.
Given target = 20
, return false
.
解法一:
二分法查找。与1不同的是,每当拿一个位置与之比之后,只能确定删除一个block,还剩下的区域至少要进行两次相同的查找才会出结果。
比如例子中,左上角是(0, 0) 右下角(4, 4)。那么第一次取到(2, 2) (9)。比如要找7, 7比9小,那么我们确定的是比9大的区域(以9为左上顶点的block)中没有我们要的元素;如果大于则相反,以该点为右下顶点的block没有目标元素。
剩下的区域可以取9的上边block:1 到 19;再取 3到21的block。 也可以先取得1到21,再取7到19。两次调用的结果要用或关系。
public class Solution { // 17ms public boolean searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length ==0 || matrix[0].length == 0) return false; return helper(matrix, target, 0, 0, matrix.length-1, matrix[0].length-1); } public boolean helper(int[][] matrix, int target, int x1, int y1, int x2, int y2){ if(x1 > x2 || y1 > y2) return false; int mx = (x1 + x2)/2; int my = (y1 + y2)/2; if(matrix[mx][my] == target) return true; else if(matrix[mx][my] > target) return helper(matrix, target, x1, y1, mx-1, y2) || helper(matrix, target, mx, y1, x2, my-1); else return helper(matrix, target, mx+1, y1, x2, y2) || helper(matrix, target, x1, my+1, mx, y2); }}
解法二:
来源这里。
从右上角开始, 比较target 和 matrix[i][j]的值. 如果小于target, 则该行不可能有此数, 所以i++; 如果大于target, 则该列不可能有此数, 所以j--. 遇到边界则表明该矩阵不含target.
真是神算法。。
public class Solution { // 13ms public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0 || matrix[0].length == 0) return false; int i = 0, j = matrix[0].length - 1; while(i < matrix.length && j >= 0) { int x = matrix[i][j]; if(target == x) return true; else if(target < x) j--; else i++; } return false; } }
- Leetcode 74. Search a 2D Matrix & 240. Search a 2D Matrix II
- leetcode题解-74. Search a 2D Matrix && 240. Search a 2D Matrix II
- #leetcode#Search a 2D Matrix II
- [LeetCode] Search a 2D Matrix II
- leetcode: Search a 2D Matrix II
- leetcode - Search a 2D Matrix II
- [leetcode] Search a 2D Matrix II
- 【leetcode】Search a 2D Matrix II
- Leetcode: Search a 2D Matrix II
- LeetCode Search a 2D Matrix II
- [Leetcode]Search a 2D Matrix II
- [LeetCode]Search a 2D Matrix II
- LeetCode Search a 2D Matrix II
- [LeetCode]Search a 2D Matrix II
- LeetCode || Search a 2D Matrix II
- LeetCode----Search a 2D Matrix II
- Leetcode: Search a 2D Matrix II
- *LeetCode-Search a 2D Matrix II
- VR运动机制详细调查报告出炉 …
- 探究VR/AR行业走势
- VR支付技术能否支撑起新的浪潮
- JAVA学习笔记17:JDBC Driver
- Java线程基础(2)-实现线程的三种基本方法
- Leetcode 74. Search a 2D Matrix & 240. Search a 2D Matrix II
- 双向管道的,两个程序之间的交互
- windows API 封装
- Debian下使用apt-get安装 Nginx + MySQL + PHPMyAdmin 备忘
- 安装windows服务
- Linux下安装python的swampy包
- 机器学习-研究人员-博客
- ngrok 安装
- Quartz 2D编程指南之七:阴影