54. Search a 2D Matrix && Climbing Stairs (Easy)
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)的更多相关文章
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 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 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- Windows 10简体中文最新预览版Build 9926
Windows 10 消费者预览版全新特性: • 全新的开始菜单Win 10的开始菜单产生了较大改变,磁贴界面在原有磁贴概念的基础上进行了大幅度的调整,新的磁贴界面开始支持纵向滚动,并可以利用开始按钮 ...
- 记录一些容易忘记的属性 -- UIScrollView
UIScrollView * sv = [[UIScrollView alloc] init]; //设置是否显示水平滚动条 sv.showsHorizontalScrollIndicator ...
- 说说chrome上的JS调试
步骤:审查元素 ->source->选择js文件 ->设置断点 ->触发函数 ->进入调试点step over 到elements目录下双击某一块代码还可以立刻修改,ct ...
- SQL.WITH AS.公用表表达式(CTE)
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...
- UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)
UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...
- Android 开发之拦截EditText的输入内容,定制输入内容
1.EditText作为一个比较成熟的View,在Android的应用开发中得到极为广泛的使用.在某些特殊情况下,我们可能需要定制EditText的输入内容, 只允许指定功能的输入,例如输入一个”dd ...
- 一个基于ANTLR 4的布尔表达式语句解释器的实现
Reference The Definitive ANTLR 4 Reference, 2nd Edition. 0 Features labeled grammar definition, i.e. ...
- python 杨辉三角
前提:端点的数为1. 每个数等于它上方两数之和. 每行数字左右对称,由1开始逐渐变大. 第n行的数字有n项. 第n行数字和为2n-1. 第n行的m个数可表示为 C(n-1,m-1),即为从n-1个不同 ...
- 由javascript中的this指针所想到的
初次结识 this 指针,是在学 <<C++ Primer Plus>>这本书的时候(这本书勉强读了一二遍,之后转学 html+css+js了,不过这是后话). 依稀记得书中举 ...
- Python学习笔记——Day2
一.集成开发环境 集成开发环境(IDE,Integrated development Enviroment)是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面等工具.集 ...