[LeetCode] Search a 2D Matrix
2013-11-12 12:23
155 查看
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.
For example,
Consider the following matrix:
Given target =
解题思路:
二分,递归。
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.
For example,
Consider the following matrix:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ]
Given target =
3, return
true.
解题思路:
二分,递归。
class Solution { public: int m = 0, n = 0; vector<vector<int>> data; bool search(int left, int right, int target) { int mid = (right + left) / 2; if(right - left == 1) return target == data[left / n][left % n]; else return search(left, mid, target) || search(mid, right, target); } bool searchMatrix(vector<vector<int> > &matrix, int target) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(matrix.size() == 0) return false; data = matrix; m = matrix.size(), n = matrix[0].size(); return search(0, m * n, target); } };
相关文章推荐
- 开始刷leetcode day40: Search a 2D Matrix
- [LeetCode] 74. Search a 2D Matrix
- leetcode ---Search a 2D Matrix
- 剑指offer 面试题3:二维数组(矩阵)中数的查找(leetcode 74. Search a 2D Matrix) 题解
- 【leetcode】Search 2D Matrix
- leetcode — search-a-2d-matrix
- Leetcode: Search a 2D Matrix
- 【leetcode】74. Search a 2D Matrix【java】
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
- leetcode74Search a 2D Matrix
- 【Leetcode】Search a 2D Matrix
- Search a 2D Matrix -- LeetCode
- LeetCode - Search a 2D Matrix
- LeetCode: Search a 2D Matrix 题解
- Leetcode题集——search-a-2d-matrix
- [leetcode]Search a 2D Matrix
- [LeetCode] Search a 2D Matrix
- 【LeetCode】 074. Search a 2D Matrix
- [LeetCode] Search a 2D Matrix 二分搜索
- LeetCode074 Search a 2D Matrix