Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。
问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找。
算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法。
- public class SearchInSortedMatrix
- {
- public static boolean searchMatrix(int[][] matrix, int target)
- {
- int m = matrix.length;
- int n = matrix[0].length;
- int low = 0;
- int high = m*n - 1;
- while(low <= high)
- {
- int mid = (low + high)/2;
- int row = mid / n;
- int column = mid % n;
- if(matrix[row][column] == target)
- {
- return true;
- }
- else if(matrix[row][column] < target)
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- return false;
- }
- }
问题描述:二维矩阵行有序,列有序,进行查找。
算法分析:有两种方法,一种是将矩阵按中心点分成左上,左下,右上,右下,四部分,进行递归查找。
还有一种比较巧妙的查找方法,就是从左下角或者右上角的元素进行查找。
- //矩阵每一行有序,每一列有序
- public boolean searchMatrix2(int[][] matrix, int target)
- {
- int m = matrix.length;
- int n = matrix[0].length;
- return helper(matrix, 0, m-1, 0, n-1, target);
- }
- public boolean helper(int[][] matrix, int rowStart, int rowEnd, int colStart, int colEnd, int target)
- {
- int rm = (rowStart + rowEnd)/2;
- int cm = (colStart + colEnd)/2;
- if(rowStart > rowEnd || colStart > colEnd)
- {
- return false;
- }
- if(matrix[rm][cm] == target)
- {
- return true;
- }
- else if(matrix[rm][cm] > target)
- {
- return helper(matrix, rowStart, rm - 1, colStart, cm - 1, target)
- || helper(matrix, rm, rowEnd, colStart, cm - 1, target)
- || helper(matrix, rowStart, rm - 1, cm, colEnd, target);
- }
- else
- {
- return helper(matrix, rm + 1, rowEnd, cm + 1, colEnd, target)
- || helper(matrix, rm + 1, rowEnd, colStart, cm, target)
- || helper(matrix, rowStart, rm, cm + 1, colEnd, target);
- }
- }
- //从右上角元素进行查找
- public boolean searchMatrix3(int[][] matrix, int target)
- {
- int m = matrix.length;
- int n = matrix[0].length;
- int low = 0;
- int high = m*n - 1;
- while(low <= high)
- {
- int mid = (low + high)/2;
- int row = mid / n;
- int column = mid % n;
- if(matrix[row][column] == target)
- {
- return true;
- }
- else if(matrix[row][column] < target)
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- return false;
- }
Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。的更多相关文章
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
随机推荐
- 第二课作业——redis常用命令
第二课时作业 静哥 by 2016.2.23~2016.2.22 [作业描述] 1.key string list hash结构中,每个至少完成5个命令,包含插入 修改 删除 查询,list 和h ...
- Web 编程中路径问题
web.xml 中 <url-pattern> 路径(即 Servlet 路径) 要么以 "*" 开头, 要么以 "/" 开头. 转发和包含路径(服 ...
- cookies设置时间
默认cookies失效时间是直到关闭浏览器,cookies失效,也可以指定cookies时间. Response.Cookies("user_name").Expires=Date ...
- 关于session的常用用法
(一)django有四中session实现方式 1.数据库(database-backed sessions) 2.缓存(cached sessions) 3.文件系统(file-based sess ...
- TouchDelegate
TouchDelegate(Rect bounds, View delegateView) Parameters: bounds Bounds in local coordinates of the ...
- Android View学习Tips
1.Canvas.save()和Canvas.restore() canvas.save();和canvas.restore();是两个相互匹配出现的,作用是用来保存画布的状态和取出保存的状态的.这里 ...
- 012-JDK可视化监控工具-jstack
一.概述 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...
- pgadmin3
一般性 \copyright 显示PostgreSQL的使用和发行许可条款 \g [文件] or; 执行查询 (并把结果写入文件或 |管道) \gset [PREF ...
- nginx常用
1.rewrite return 301 http://example.com$request_uri; rewrite ^ http://example.com permanent; 2.try_f ...
- GIL解释器,协程,gevent模块
GIL解释器锁 在Cpython解释器中,同一个进程下开启的多线程,同一时刻只能有一个线程执行,无法利用多核优势 首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CP ...