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 row=matrix.size();
int col=matrix[].size();
vector<int> m(row*col); for(int i=;i<row;i++)
{
for(int j=;j<col;j++)
{
m[i*col+j]=matrix[i][j];
}
} int left=;
int right=m.size()-;
int mid;
while(left<=right)
{
mid=(left+right)/;
if(m[mid]>target)
right=mid-;
else if(m[mid]<target)
left=mid+;
else
return true;
}
return false; }
};
 
 
第二种思路,直接使用二分法
 
把一维数字转化为二维的坐标的方法:
第n个元素,在n/col行,n%col列
 
 class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) { int row=matrix.size();
int col=matrix[].size(); int left=;
int right=row*col-;
int mid;
int m;
while(left<=right)
{
mid=(left+right)/;
m=matrix[mid/col][mid%col];
if(m>target)
right=mid-;
else if(m<target)
left=mid+;
else
return true;
}
return false;
}
};

【leetcode】Search a 2D Matrix的更多相关文章

  1. 【leetcode】 Search a 2D Matrix (easy)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  2. 【Leetcode】【Medium】Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. 【数组】Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

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

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

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

  6. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

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

  8. 【题解】【数组】【查找】【Leetcode】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

随机推荐

  1. 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  2. IntelliJ IDEA 远程调试

    远程调试服务器是一个比较实用的技巧,以便我们能够迅速定位线上问题.本文会介绍如何在IntelliJ IDEA中进行远程调试. 配置IntelliJ IDEA 选择Edit Configurations ...

  3. application

    改变全局application到页面的参数 添加参数:HttpServletRequest req 使用req.getSession().getServletContext().setAttribut ...

  4. (转载)ecshop制作成手机网站的方法

    ecshop用手机访问的时候,会自动跳转到  /mobile 目录下,ecshop自带的wap模板是用wml制作的,如果按这种情况,又需要制作一套模板,太麻烦,现在都是智能手机时代,wml模板已经不能 ...

  5. 新浪微博客户端(63)-使用block进行链式编程

    Person.h #import <Foundation/Foundation.h> @interface Person : NSObject - (Person *(^)())study ...

  6. firefox的plugin-container.exe进程如何关闭?

    为什么要关闭container进程? 查看firefox所消耗的资源: ff本身: cpu一般是0-10%, 内存一般是400MB左右 plugin-container: cpu所占的比例很高, 可达 ...

  7. 【最新】2015年7月之15个最新jQuery插件

    Hello,一个激动人心的好消息,现在我为大家整理最近7月发布的jQuery插件. 如果你熟悉任何下面列出的插件,请分享你的反馈与我们的读者,或如果你知道哪一个我们没有收录,那么请与我们分享在下面的评 ...

  8. 新手使用R的注意事项

    1.最好先设置工作目录 如: setwd(“D:/DataDig”) 注意不是”\”,是”/” 再读取数据,如: datafile = read.csv("./test.csv") ...

  9. NHibernate概念

    SessionFactory (NHibernate.ISessionFactory) 对属于单一数据库的编译过的映射文件的一个线程安全的,不可变的缓存快照.它是Session的工厂,是Connect ...

  10. Mac Pro 修改环境变量

    参考:Ubuntu 12 修改环境变量 [实战] 把 php.php-fpm.nginx.mysql 的相关命令路径添加到 用户环境变量 $ vim ~/.bash_profile alias ll= ...