74. Search a 2D Matrix && 240. Search a 2D Matrix II
2016-06-23 14:32
316 查看
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.
For example,
Consider the following matrix:
Given target =
Array Binary Search
Hide Similar Problems
(M) Search a 2D Matrix II
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:
Given target =
Given target =
O(M+N) solution:
2D Binary Search. Divide and Conquer (log4/3(M*N)).
M*log2(N): Do the 1D binary search for each row.
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] ]
Given target =
3, return
true.
Array Binary Search
Hide Similar Problems
(M) Search a 2D Matrix II
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int row_num = matrix.length; int col_num = matrix[0].length; //Treat it as a one-dimension array. int begin = 0, end = row_num * col_num - 1; while(begin <= end){ int mid = (begin + end) / 2; int mid_value = matrix[mid/col_num][mid%col_num]; if( mid_value == target){ return true; }else if(mid_value < target){ begin = mid+1;//Otherwise infinite loop. }else{ end = mid-1; } } return false; } }
240. Search a 2D Matrix II
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 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.
O(M+N) solution:
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length < 1 || matrix[0].length <1) { return false; } int col = matrix[0].length-1; int row = 0; while(col >= 0 && row <= matrix.length-1) { if(target == matrix[row][col]) { return true; } else if(target < matrix[row][col]) { col--; } else if(target > matrix[row][col]) { row++; } } return false; } }
2D Binary Search. Divide and Conquer (log4/3(M*N)).
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { return searchMatrix(matrix, target, 0, matrix.length-1, 0, matrix[0].length-1); } //[rFrom, rTo], [cFrom, cTo] private boolean searchMatrix(int[][] matrix, int target, int rFrom, int rTo, int cFrom, int cTo) { if(rFrom > rTo || cFrom > cTo) return false; if(rFrom == rTo && cFrom == cTo) { if(matrix[rFrom][cFrom] == target) return true; return false; } int rMid = (rFrom+rTo)/2; int cMid = (cFrom+cTo)/2; if(matrix[rMid][cMid] == target) return true; if(matrix[rMid][cMid] < target) return searchMatrix(matrix, target, rFrom, rTo, cMid+1, cTo) || // right side region searchMatrix(matrix, target, rMid+1, rTo, cFrom, cMid); //lower left region return searchMatrix(matrix, target, rFrom, rMid-1, cFrom, cTo) || //up side region searchMatrix(matrix, target, rMid, rTo, cFrom, cMid-1); //lower left region } }
M*log2(N): Do the 1D binary search for each row.
相关文章推荐
- 错误
- edgesForExtendedLayout属性
- 网络请求框架
- Leetcode 131 Palindrome Partitioning
- Android安全开发之ZIP文件目录遍历
- Intent Flag实际项目 -- 超时跳转登录界面并清理前面所有activity
- [LeetCode]problem 148. Sort List
- Java开发工具集---Guava
- JVM内部结构
- 欧拉路径 LeetCode 332
- 整数划分
- Material Design Library系列之CoordinatorLayout
- string 的简单实现
- Android中线程那些事儿
- 【css 效果】边沿打孔的优惠券
- Flink 另外一个分布式流式和批量数据处理的开源平台
- mac tool
- [Android Tips] 19. Android Studio Plugins
- 【css 效果】边沿打孔的优惠券
- 【httplistener监听获取Post请求参数】