【Leetcode】74. Search a 2D Matrix
来源:互联网 发布:电信网络限制 编辑:程序博客网 时间:2024/05/16 09:14
Description:
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.
Example:
Consider the following matrix:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
Given target = 3, return true.
Solution:
本题实际是对矩阵进行一次查找,如果进行逐个遍历,时间复杂度会是O(
在本题,由于操作的是二维矩阵,我们需要得到二维矩阵中间的数。一维数组和二维矩阵
class Solution{ public: bool searchMatrix(vector<vector<int> >& matrix, int target) { int row = matrix.size(); int column = matrix[0].size(); int left = 0; int right = row*column-1; while (left != right){ int mid = (l + r - 1) >> 1;//相当于做除以2运算,使用右移运算符效率更高 if (matrix[mid/column][mid%column] < target) left = mid + 1; else right = mid; } return (matrix[right/column][right%column] == target); } }};
- LeetCode 74. Search a 2D Matrix
- [LeetCode]74.Search a 2D Matrix
- LeetCode --- 74. Search a 2D Matrix
- [Leetcode] 74. Search a 2D Matrix
- [leetcode] 74.Search a 2D Matrix
- Leetcode 74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- 74. Search a 2D Matrix LeetCode
- [LeetCode]74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- leetcode 74. Search a 2D Matrix
- LeetCode 74. Search a 2D Matrix
- LeetCode 74. Search a 2D Matrix
- LeetCode *** 74. Search a 2D Matrix
- leetcode 74. Search a 2D Matrix
- LeetCode 74. Search a 2D Matrix
- leetcode-74. Search a 2D Matrix
- [leetcode] 74. Search a 2D Matrix
- 深入分析 Java I/O 的工作机制
- 2017企业专利申请的步骤是什么?专利申请流程有哪些?
- 移动app测试中的基本要求
- TreeMap、TreeSet
- WebView小知识点记录
- 【Leetcode】74. Search a 2D Matrix
- Exception sending context initialized event to listener instance of class org.springframework.web.co
- Java面试之垃圾回收总结
- class文件JDK版本查看
- resultMap 多对多查询
- 自定义圆内套三角形
- ORACLE 学习笔记 -->存储过程,游标等
- 理解Maven中的SNAPSHOT版本和正式版本
- bzoj1651