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 ...
随机推荐
- corethink功能模块探索开发(十五)后台新增按钮
效果图: 1.首先添加数据列表上的新增按钮,让按钮显示出来 ->addTopButton('addnew') 2.让这个按钮行动起来,实现add方法 public function add(){ ...
- shell脚本调用传参【转载】
转自:https://www.cnblogs.com/cisum/p/8010658.html 1.直接使用$0,$1,$2,$3 $0是脚本的名字,就是按顺序来 #!/bin/bash # auth ...
- python16_day17【Django_session、ajax】
一.Session 1.settings.py SESSION_ENGINE = 'django.contrib.sessions.backends.db' # 引擎(默认) SESSION_COOK ...
- Visio Yoeman
Visio需要确定文件位置才能运行的 Yo主要是用来生成框架的,相当于是一个框架生成器 new install -g generator-django 然后在Visio按F5,选择要创建的环境,就可以 ...
- PAT 天梯赛 L1-008. 求整数段和 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-008 AC代码 #include <iostream> #include <cstdio&g ...
- Firebug入门指南(转)
本文转自:http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 作者: 阮一峰 日期: 2008年6月 8日 据说,对于网页开发人员 ...
- mysql一次性删除所有表而不删除数据库
1.执行如下语句获取删除语句 SELECT CONCAT( 'drop table ', table_name, ';' ) from information_schema.tables where ...
- Autofac register and resolve
Passing Parameters to Register When you register components you have the ability to provide a set of ...
- 牛的障碍Cow Steeplechase
题目描述 Farmer John has a brilliant idea for the next great spectator sport: Cow Steeplechase! As every ...
- XML基本知识点——思维导图
如图 思维导图图片链接 http://www.edrawsoft.cn/viewer/public/s/5dcd3224563939 有道云笔记图片链接 http://note.youdao.com/ ...