LeetCode OJ-- Search a 2D Matrix
https://oj.leetcode.com/problems/search-a-2d-matrix/
具有数据递增性质的一个二维数组,对它进行二分搜索。
首先搜索target所在的行,再找列。
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if(matrix.size() == )
return false;
size_t col = matrix[].size();
if(col == )
return false;
//first find row
int lowRow = , highRow = matrix.size() - ;
int targetRow = ;
while(lowRow <= highRow && highRow >= && lowRow< matrix.size())
{
int midRow = (lowRow + highRow)/;
if(target < matrix[midRow][])
{
highRow = midRow - ;
if(highRow < )
return false;
}
else if(target == matrix[midRow][] || target == matrix[midRow][col - ])
return true;
else if(col!= && target < matrix[midRow][col - ])
{
targetRow = midRow;
break;
}
else
{
lowRow = midRow + ;
if(lowRow >= matrix.size())
return false;
}
}
//find col binary search
int lowCol = , highCol = col;
while(lowCol <= highCol)
{
int midCol = (lowCol + highCol)/;
if(matrix[targetRow][midCol] == target)
return true;
else if(matrix[targetRow][midCol] < target)
lowCol = midCol + ;
else
highCol = midCol - ;
}
return false;
}
};
LeetCode OJ-- Search a 2D Matrix的更多相关文章
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 74. Search a 2D Matrix 解题思路
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- leetcode 【Search a 2D Matrix 】python 实现
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- leetcode 74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 排序算法C语言实现——插入排序(优于冒泡)
为什么插入排序要优于冒泡? 插入排序在于向已排序序列中插入新元素,主要的动作是移动元素,涉及1次赋值,即data[j] = data[j-1]; 而冒泡排序在于相邻元素交换位置,涉及3条赋值,即iTm ...
- 【转】Python操作MongoDB
Python 操作 MongoDB 请给作者点赞--> 原文链接 这篇文章主要介绍了使用Python脚本操作MongoDB的教程,MongoDB作为非关系型数据库得到了很大的宣传力度,而市面 ...
- 如何在C#中调试LINQ查询
原文:How to Debug LINQ queries in C# 作者:Michael Shpilt 译文:如何在C#中调试LINQ查询 译者:Lamond Lu 在C#中我最喜欢的特性就是LIN ...
- “帮你APP”团队冲刺6
1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...
- laravel5.2总结--文件上传
1 配置 文件系统的配置文件在 config/filesystems.php 文件中,此处我们新建一个uploads本地磁盘空间用于存储上传的文件,具体配置项及说明如下: <?php retur ...
- 【Best Time to Buy and Sell Stock】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 【3Sum Closest 】cpp
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- leetcode 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- C++ STL map容器的说明测试1
// maptest.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" /*********************************** ...