LeetCode OJ:Search a 2D Matrix(二维数组查找)
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:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3
, return true
.
二分法的简单变形,把整个数组当成一个长的大数组就可以了,代码如下:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int szHor = matrix.size();
if(!szHor) return false;
int szVer = matrix[].size();
if(!szVer) return false;
int totalSize = szHor * szVer;
return bs(matrix, , totalSize - , target, szVer); } bool bs(vector<vector<int>> & matrix, int beg, int end, int target, int colCount)
{
if(beg > end)
return false;
int mid = beg + (end - beg)/;
int val = matrix[mid/colCount][mid%colCount];
if(val == target) return true;
else if(val < target)
return bs(matrix, mid + , end, target, colCount);
else
return bs(matrix, beg, mid - , target, colCount);
} };
LeetCode OJ:Search a 2D Matrix(二维数组查找)的更多相关文章
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- leetcode——Search a 2D Matrix 二维有序数组查找(AC)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [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】剑指 Offer 04. 二维数组中的查找
二维数组查找:线性查找法 有二维数组: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, ...
- php利用array_search与array_column实现二维数组查找
利用array_search与array_column实现二维数组查找,不用自己写个循环,减少工作量. <?php $userdb = array( 0 => array( 'uid' = ...
- (2)剑指Offer之二维数组查找和替换空格问题
一 二维数组查找 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 问 ...
- [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(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- 【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 ...
随机推荐
- Generating Gaussian Random Numbers(转)
Generating Gaussian Random Numbers http://www.taygeta.com/random/gaussian.html This note is about th ...
- 如何在浏览器网页中显示word文件内容
如何在浏览器网页中显示word文件内容 把word文件读到byte[]中,再Response.OutputStream.Write(bytes)到客户端去 Page_Load事件中写: //FileS ...
- beego——静态文件
Go 语言内部其实已经提供了 http.ServeFile,通过这个函数可以实现静态文件的服务. beego 针对这个功能进行了一层封装,通过下面的方式进行静态文件注册: beego.SetStati ...
- redis.conf配置项说明
#是否以后台进程运行,默认为no,如果需要以后台进程运行则改为yes daemonize no #如果以后台进程运行的话,就需要指定pid,你可以在此自定义redis.pid文件的位置. pidfil ...
- C# Json格式
using LitJson; //自定义Json类 JsonDataResult jsondata = new JsonDataResult() { Success = false }; HttpCo ...
- HTML5堆木头游戏
在线演示 本地下载
- sed 案例
sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器.能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上 ...
- Docker入门使用
先来一波docker的指令: docker inspect 容器id 查询容器信息 docker stop 容器id 停止容器id docker rm 容器id ...
- Sublime Text3 使用记录
Sublime Text 3 使用记录 来看下本文的大纲吧 介绍下,我的环境 操作系统:win10 64bit sublime Text 3 版本:3143 那么就开始啦. 一. ...
- 基于开源库jsoncpp的json字符串解析
json(JavaScript Object Notation)是一种轻量级高效数据交换格式.相比于XML,其更加简洁,解析更加方便.在实习期间,我负责的程序模块,多次使用到json进行数据传输.由于 ...