LintCode 28 搜索二维矩阵
来源:互联网 发布:新闻网站源码 手机版 编辑:程序博客网 时间:2024/06/06 02:47
题目:searchMatrix
要求:
写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每行的第一个数大于上一行的最后一个整数。
样例:
考虑下列矩阵:[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]给出 target = 3,返回 true
算法要求:
O(log(n) + log(m)) 时间复杂度
解题思路:
直接判断数组末尾的是否大于目标,如果大于,则说明目标数值可能在这一行,或者不存在。
算法如下:
bool searchMatrix(vector<vector<int> > &matrix, int target) { int size = matrix.size(); for (int i = 0; i < size; i++) { int size2 = matrix[i].size(); if (target > matrix[i][size2 - 1]) { continue; } for (int j = 0; j < size2; j++) { if (target == matrix[i][j]) { return true; } } } return false; }
0 0
- LintCode 28 搜索二维矩阵
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 【LintCode】搜索二维矩阵
- lintcode,搜索二维矩阵
- LintCode : 搜索二维矩阵
- Lintcode:搜索二维矩阵
- (lintcode)第28题 搜索二维矩阵
- LintCode之28 搜索二维矩阵
- LintCode:搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- 【LintCode】搜索二维矩阵 II
- lintcode,搜索二维矩阵 II
- Lintcode 搜索二维矩阵 II
- [LintCode]28.搜索二维矩阵
- lintcode---搜索二维矩阵Ⅱ
- lintcode-搜索二维矩阵II-38
- 搜索二维矩阵——LintCode
- CodeForces 317D Game with Powers
- Java控制循环语句break、continue、return介绍
- 使用HashSet存储10个1-20之间的随机数,并且要求随机数不重复。
- java中this的用法(2)
- Bayesian trained NN
- LintCode 28 搜索二维矩阵
- leetcode 387. First Unique Character in a String
- MySQL系列之八:中间件
- 08-进程间通信概述
- core I/O class plus all decorators Decorator design pattern,
- redis-key
- android错误之android.content.res.Resources$NotFoundException:
- iOS
- 深度学习(Deep Learning) 5.CNN