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. 字符串中带有emoji表情处理

    1:先删除字符然后解析当前字符再显示 edit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextC ...

  2. Excel 2007 批量删除隐藏的文本框[转]

    该方法取自百度知道,该朋友给出函数,我详细写一下方法. 打开有文本框的excel文件. 按 Alt+F11 打开编辑器. 将下面的函数复制进去: Sub deltxbox()Dim s As Shap ...

  3. Java中正则表达式及其常用类Math、Calendar、Date、BigDecimal、BigInterger、System、Rondom的使用

    1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ( ...

  4. iOS 中捕获程序崩溃日志

    iOS 中捕获程序崩溃日志 (2014-04-22 17:35:59) 转载▼     iOS开发中遇到程序崩溃是很正常的事情,如何在程序崩溃时捕获到异常信息并通知开发者,是大多数软件都选择的方法.下 ...

  5. javascript笔记7-事件

    主要讲事件流.事件捕获.事件冒泡.事件处理程序.事件属性.事件类型.内存和优化等. 由于本文已经在微信订阅号上发布,为了防止原创性冲突检测,因此本文在此处已经删除. 详细请扫描订阅号二维码,查看历史信 ...

  6. SQL分类

    SQL(Structure Query Language)结构化查询语言,是使用关系型数据库的应用语言. SQL主要可以划分为以下三个类别: DDL(Data Define Language)语句:数 ...

  7. jQuery 中 offset()方法与用position()的区别

    jq中offset().left和offset().top获取的是相对于整个文档左上角的偏移. 而用$(selector).position().left和.top 取到的则是相对于selector父 ...

  8. swiper中提供的动画效果

    目前就只有这些,大家也可以尝试自己写一些想要的效果.动手试试,才能清楚每个效果具体是怎么回事~ bounce:弹跳两下出来flash:闪烁两下pulse:脉冲形式出来rubberBand:橡皮圈形式弹 ...

  9. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  10. mongodb,redis,hbase 三者都是nosql数据库,他们的最大区别和不同定位是什么?

      不严谨地讲,Redis定位在"快",HBase定位于"大",mongodb定位在"灵活". NoSQL的优点正好就是SQL的软肋,而其弱 ...