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 ...
随机推荐
- 005-环境安装【docker、fabric】
1.参考地址:https://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html#install-curl 一.前置条件和系统配置 1.安 ...
- 解决 hybird 应用中重复获取 WebView,导致页面元素无法识别的问题
转载地址:http://blog.csdn.net/testman930/article/details/50799532 问题描述 在测APP的业务流,WebView和Native模式耦合在一起.例 ...
- APP 自动化测试封装结构模式
原文出处http://www.toutiao.com/a6268089772108333314/ 做过UI自动化测试同学,都会深深体会几个痛点:维护量大.适配量大.编写代码巨大等.基于这些问题,大家都 ...
- Delphi 正则表达式语法(9): 临界匹配 - 也叫"预搜索"与"反向预搜索"
Delphi 正则表达式语法(9): 临界匹配 - 也叫"预搜索"与"反向预搜索" //匹配右边 var reg: TPerlRegEx; begin ...
- 简单的menu和点击(包括alertDialog定制)
import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android. ...
- 28. Implement strStr()(KMP字符串匹配算法)
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- cdoj1328卿学姐与诡异村庄
地址:http://acm.uestc.edu.cn/#/problem/show/1328 题目: 卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others) ...
- 让Jackson JSON生成的数据包含的中文以unicode方式编码
本文出处:http://blog.csdn.net/chaijunkun/article/details/8257209,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建 ...
- C# winform 屏蔽鼠标右键 spreadsheet Gear 屏蔽鼠标右键菜单
今天用到spreadsheetGear 插件,然后右键有插件自己的菜单.都是英文的,而且还能打开新的窗体.嵌到程序里面,不太合适,所以着手屏蔽. 刚开始用的Mouse_up,虽然能捕获事件,但是没有K ...
- [转]React Native 语言基础之ES6
React Native 是基于 React 这个前端框架来构建native app的架构.React Native基于ES6(即ECMAScript2015)语言进行开发的. JS的组成 1) 核心 ...