74. Search a 2D Matrix

整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了。

这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,%col获得y坐标

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
if(row <= )
return false;
int col = matrix[].size();
if(col <= )
return false;
int start = ;
int end = row*col - ;
int mid;
while(start + < end){
mid = start + (end - start)/;
int r = mid/col;
int c = mid%col;
if(matrix[r][c] == target)
return true;
else if(matrix[r][c] < target)
start = mid;
else
end = mid;
}
if(matrix[start/col][start%col] == target)
return true;
if(matrix[end/col][end%col] == target)
return true;
return false;
}
};

240. Search a 2D Matrix II

与第一个题不同,行与行之间不是严格递增。剑指上的原题,从最左下角或者最右上角都可以

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if(m <= )
return false;
int n = matrix[].size();
if(n <= )
return false;
int i = ,j = n-;
while( i < m && j >= ){
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] > target)
j--;
else
i++;
}
return false;
}
};

leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II的更多相关文章

  1. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

  2. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  3. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  4. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  5. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  6. [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 ...

  7. [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 ...

  8. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 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 ...

随机推荐

  1. js 字符串转数组

    var obj = "123456".replace(/(.)(?=[^$])/g, "$1,").split(",");    conso ...

  2. vuejs通过filterBy,orderBy实现搜索筛选,降序排序数据实例

    直接贴代码了: 先上输入前的样子: <style> #example{margin:100px auto;width:600px;} .show{margin:10px;} #search ...

  3. Ubuntu添加新分区

    1.查看分区 ~$ sudo fdisk -l 2.添加分区 ~$ fdisk /dev/sda 3.输入m查看帮助 ~$ m 4.输入n新建分区,即添加分区 ~$ n 这里会出现设置分区大小 5.输 ...

  4. android 圆角背景

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  5. (后端)Spring Boot自定义错误页面,Whitelabel Error Page处理方式(转)

    我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一 ...

  6. 在Windows服务中托管 ASP.NET Core的坑

    按照官网教程 https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/windows-service?view=aspnetcore- ...

  7. 安卓程序中手机后退键与标题栏后退键是不同的,前者回出发onBackPressed()函数,后者需要重重写temclick函数

    安卓程序中手机后退键与标题栏后退键是不同的,前者回出发onBackPressed()函数,后者需要重重写temclick函数

  8. mssql sql server ceiling floor 函数用法简介

    摘自: http://www.maomao365.com/?p=5581摘要: 下文主要讲述ceiling.floor函数的功能及举例说明  一.ceiling floor函数功能简介 ceiling ...

  9. MongoDB数据创建与使用

    MongoDB数据创建与使用 创建数据库 代码功能:读取本地文本文件,并保存到数据库中 import pymongo #连接mongo数据库 client = pymongo.MongoClient( ...

  10. 光盘yum源搭建

    先修改基本的yum原源,使其不生效 cd /etc/yum.repos.d/ mv CentOS-Base.repo CentOS-Base.repo.bak 在修改媒介yum源使其生效 检验是否生效 ...