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.

思路: 从右上方开始,若小于 target, 则往下走;若大于 target, 对改行二分查找;若等 target, 返回 true.

bool binarySearch(vector<int> &A, int target) {
int l = 0, h = A.size()-2;
while(l <= h) {
int mid = (l+h) >> 1;
if(A[mid] > target) h = mid-1;
else if(A[mid] < target) l = mid+1;
else return true;
}
return false;
}
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if(!matrix.size() || !matrix[0].size()) return false;
int row = matrix.size(), col = matrix[0].size();
for(int r = 0; r < row; ++r) {
if(matrix[r][col-1] == target) return true;
if(matrix[r][col-1] > target) return binarySearch(matrix[r], target);
}
return false;
}
};

Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

思路:  斐波那契。此处用动归。 还可以使用矩阵二分乘。(剑指offer: 题9)

// Fibonacci
class Solution {
public:
int climbStairs(int n) {
vector<int> f(n+1, 0);
f[0] = f[1] = 1;
for(int i = 2; i <= n; ++i) f[i] = f[i-1] + f[i-2];
return f[n];
}
};

54. Search a 2D Matrix && Climbing Stairs (Easy)的更多相关文章

  1. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  2. leetcode-746-Min Cost Climbing Stairs(动态规划)

    题目描述: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...

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

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

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

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

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

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

  6. Search a 2D Matrix | & II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...

  7. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

  8. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

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

  9. [LeetCode] 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. Android中Handler的消息处理

    关于Handler机制,能找到无数的文章.http://blog.csdn.net/jiangshitian/article/details/12525313这篇博客写得比较好,关键是图很清晰,结合H ...

  2. xcode代码提示功能失效的解决方法

    xcode 自动提示很好用 然而大量的工作也是让他吃不消了 结果今天提示功能给我来了个罢工 这当然是不行的 也是万能的搜索帮我解决了这个问题 方法很多 选择了简单的 xcode --> Wind ...

  3. ubuntu auto install update

    sudo apt-get update sudo apt-get dist-upgrade 32bit mode sudo dpkg --add-architecture i386

  4. 【转】局域网内访问VS2012 调试的IIS Express web服务器

    1.修改发布项目的web属性 2.在我的文档中打开IISExpress\config\applicationhost.config 加上下面的一句 3.重启调试 转自:http://blog.chin ...

  5. 快速排序C++

    /* * quick_sort.cpp * * Created on: 2016-3-21 * Author: Lv_Lang */ //快速排序 #include <iostream> ...

  6. Apache2.2与php5.17 mysql配置

    php5.217应该用线程安全搬,不然各种无语的Apache打不开,PHPInfo没有Mysql的信息,记得把php.ini放入系统盘Windows目录下,Win764位的libmysql.dll也放 ...

  7. php7+apache2.4 (Windows7下),成功启动。(楼主另外提供了1个php7集成环境打包: http://pan.baidu.com/s/1qXwjpF2 ,如果你只是想了解一下,放在d盘根目录。)

    php7正式版已经发布,性能是php5.4的2倍.博主入手php7 新鲜了一把,下面是解决问题之后成功启动php7的记录. ( 电脑必须win7 sp1, .netframework4 ) Windo ...

  8. 用文件模拟CMOS保存数据

    Hi3520D 芯片的内置CMOS最多只有5个字节可以用,但是我需要保存的数据有很多. 其中一个解决办法是:可以把其他需要保存的数据放到一个配置文件中.每次写的时候写到配置文件,用的时候再从配置文件读 ...

  9. 帝国CMS【操作类型】说明详解

    看标签的参数时候,一般最后一个参数是操作类型说明,可是后面写的是:"操作类型说明 具体看操作类型说明", 这个操作类型说明在什么地方看啊 操作类型 说明 操作类型 说明 0 各栏目 ...

  10. Map/Reduce个人实战--生成数据测试集

    背景: 在大数据领域, 由于各方面的原因. 有时需要自己来生成测试数据集, 由于测试数据集较大, 因此采用Map/Reduce的方式去生成. 在这小编(mumuxinfei)结合自身的一些实战经历, ...