Leetcode 74 Search a 2D Matrix 有序二维数组二叉查找
来源:互联网 发布:union软件安卓版 编辑:程序博客网 时间:2024/04/30 04:35
题目原址:https://leetcode.com/problems/search-a-2d-matrix/
题目描述
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.
翻译:
在一个二维数组中查找是否存在某元素,该二维数组具有以下特性:
- 每一行中的元素都是递增的
- 每一行行中最小的元素比上一行最大的元素大
即,二维数组中元素从左到右从上到下递增,而且此处我们认为矩阵中不存在重复的元素。
解题思路
此处,我们考虑使用二叉查找。
算法描述
- 根据最后一列中的元素,通过二叉查找先确定目标元素可能在哪一行中
- 在可能存在的行中进行二叉查找
代码
// 在矩阵中找目标数字// 矩阵中每一行中元素递增// 第i+1行中最小的元素比第i行中最大的元素大// 二叉查找bool searchMatrix(int** matrix, int matrixRowSize, int matrixColSize, int target) { int start = 0, end = matrixRowSize - 1, center; // 先根据每一行的最后一个元素进行二叉查找,确定目标元素可能在哪一行 while (start < end) { // center为待测范围中间的那一行 center = (start + end) / 2; // 如果第center行的尾元素就是target,直接返回true if (*(*(matrix + center) + matrixColSize - 1) == target) return true; // 如果第center行的尾元素比target小,target必在center之后的行中 else if (*(*(matrix + center) + matrixColSize - 1) < target) start = center + 1; // 如果center行的尾元素比target大,则需根据center行的首元素来继续处理 // 如果center行首元素就是target,直接返回true else if (*(*(matrix + center)) == target) return true; // 如果center行的首元素比target小,说明target只会存在center行中 else if (*(*(matrix + center)) < target) start = end = center; // 如果center行的首元素比target小,则target必在center之前的行中 else end = center - 1; } // 此时start=end,且其值为target可能存在的行 // 接下来在第row行中用二叉查找方法查找target int *row = *(matrix + start); // 目标可能存在的行 start = 0; end = matrixColSize - 1; while (start < end) { // center为中间的位置 center = (start + end) / 2; // 如果中间的元素就是target,直接返回true if (*(row + center) == target ) return true; // 如果中间的元素比target小,则缩小范围,在后半部分中查找 else if (*(row + center) < target) start = center + 1; // 如果中间的元素比target大,则缩小范围,在前半部分中查找 else end = center - 1; } // 执行到这里,start=end // 该位置的元素是target的话则查找成功,否则查找失败 return *(row + start) == target;}
测试数据
matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
选0-51为待查找数字则,测试结果为(放在数组result中,1=true,0=false)result = [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, // 0-9 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, // 10-19 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, // 20-29 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, // 30-39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-49 1, 0] // 50-51]
// 个人学习记录,若有错误请指正,大神勿喷
// sfg1991@163.com
// 2015-05-06
0 0
- Leetcode 74 Search a 2D Matrix 有序二维数组二叉查找
- leetcode——Search a 2D Matrix 二维有序数组查找(AC)
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
- Search a 2D Matrix 在有序二维矩阵查找数 @LeetCode
- [LeetCode]—Search a 2D Matrix 有序二维矩阵中查找目标值
- 字符串算法——二维有序数组中查找目标值(Search a 2D Matrix)
- (二维数组中查找target)LeetCode#74. Search a 2D Matrix #240. Search a 2D Matrix II
- Search a 2D Matrix(在二维数组中查找)
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
- 【LeetCode】Search a 2D Matrix && 【九度】题目1384:二维数组中的查找
- Leetcode刷题记—— Search a 2D Matrix(在二维数组里查找)
- [leetcode 240/74] Search a 2D Matrix II -----在左右有序,上下有序中查找数据
- LeetCode-74-Search a 2D Matrix 二维二分
- LeetCode 查找 Search a 2D Matrix
- 剑指offer 面试题3:二维数组(矩阵)中数的查找(leetcode 74. Search a 2D Matrix) 题解
- LeetCode-74-Search a 2D Matrix(二分查找)-Medium
- LeetCode | Search a 2D Matrix(二维矩阵中查找)
- LeetCode OJ 之 Search a 2D Matrix II (二维矩阵查找)
- vs2012编译使用lua 5.2静态库
- Intent 传递中 Bundle与intent.putExtra 的关系
- 牡丹江的水题们
- ZOJ 3593 One Person Game (扩展欧几里得)
- linux下安装nginx
- Leetcode 74 Search a 2D Matrix 有序二维数组二叉查找
- Spring3学习笔记之(spring core之DI配置使用1)
- CSU 1607: Do You Have The Template?(树链剖分)边权
- 整理牛人看文献的方法
- 研究生、科研人员须知的文献管理软件及一个学术会议网站
- Git 命令速查手册
- cocos2dx 3.2在Eclipse上打包出现error: call of overloaded 'abs(float)' is ambiguous
- Cookie使用时需要注意个数及大小限制
- 1--黑马程序员--7k面试题之银行业务系统