搜索二维矩阵
来源:互联网 发布:java泛型里的? 编辑:程序博客网 时间:2024/06/14 02:05
写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
每行中的整数从左到右是排序的。
每行的第一个数大于上一行的最后一个整数。
样例
考虑下列矩阵:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
给出 target = 3,返回 true
思路:
排序数组,二分查找即可;
bool searchMatrix(vector<vector<int> > &matrix, int target) { if (matrix.size() == 0)return false; int m = matrix.size(), n = matrix[0].size(); int low = 0, high = m * n - 1; int mid = 0; while (low <= high){ mid = low + (high - low) / 2; int temp = matrix[mid / n][mid % n]; if (temp == target)return true; else if (temp > target)high = mid - 1; else low = mid + 1; } return false;}
阅读全文
0 0
- 题目:搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵 II
- 搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 搜索二维矩阵II
- 【LintCode】搜索二维矩阵
- 搜索二维矩阵
- lintcode,搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵 II
- LintCode : 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- Hessian矩阵与牛顿法
- 给自己的承诺
- java语言基础-方法的递归操作
- mysql之 Innobackupex(全备+增量)备份恢复
- Markdown 安装图解(破解汉化教程)
- 搜索二维矩阵
- HDU3746--KMP+最小循环节
- 编译 链接和加载
- c++ 11 特性随笔
- 文章标题
- java语言基础-引出数组和数组定义
- bzoj1768: [Ceoi2009]logs
- HDU 5533 Dancing Stars on Me 计算几何瞎暴力
- ListView列表控件的介绍和性能优化