- 浏览: 172633 次
- 性别:
- 来自: 济南
文章分类
最新评论
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?
爬楼梯的问题,思路和Unique Paths问题类似。我们假设到了第i个台阶,因为每次只能走一步和两步,因此到第i个台阶的方式等于到第i-1个台阶的方式和到第i-2个台阶的方式的和,这时i > 1; 递推式为dp[i] = dp[i - 1] + dp[i - 2];实现代码如下:
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
爬楼梯的问题,思路和Unique Paths问题类似。我们假设到了第i个台阶,因为每次只能走一步和两步,因此到第i个台阶的方式等于到第i-1个台阶的方式和到第i-2个台阶的方式的和,这时i > 1; 递推式为dp[i] = dp[i - 1] + dp[i - 2];实现代码如下:
public class Solution { public int climbStairs(int n) { if(n <= 0) return 0; if(n == 1) return 1; if(n == 2) return 2; int[] dp = new int[n]; dp[0] = 1; dp[1] = 2; for(int i = 2; i < n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n - 1]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 223Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 221You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 338Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 329Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 446Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 426Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 424The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 382Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 513Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 526Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 857Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 877Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 549Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 595Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 760Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 728You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 625For a undirected graph with tre ...
相关推荐
Carimus编码问题代码function climbingStairs(stairs: number) { if (stairs <= 2) return stairs; return climbingStairs(stairs - 1) + climbingStairs(stairs - 2); } function climbingStairsDriver(stairs: ...
Min Cost Climbing Stairs [746]2. Best Time to Buy and Sell Stock [121]3. Climbing Stairs [70]4. Maximum Subarray [53]5. House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. ...
Stairs](./leetcode/动态规划-Climbing Stairs.java) [动态规划-Decode Ways](./leetcode/动态规划-Decode Ways.java) [动态规划-Distinct Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态...
Stairs).md](Java基础/数据结构与算法/LeetCode/LeetCode 70. 爬楼梯(Climbing Stairs).md) 7月3号 java 异常详解 7月2号 整理基础算法 7月1号 七月 6月30号 6月29号 6月25号 6月22号 Redis中String、List、Hash...
Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self Game of Life Increasing ...
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag ...Climbing Stairs Easy 动态规划 0075 Sort Colors M
70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to ...
70.爬楼梯 (Climbing Stairs) 83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of ...
第 482 章编码测试 JewelsAndStones_771 [Java] Java HashMap、ArrayList 包含方法性能对比 LicenseKeyFormatting_482 [Java] String、StringBuffer、StringBuilder 的区别和优缺点 ...ClimbingStairs_70 Q
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode 解题的原始档,包含解题中遇到的错误,也包含解出AC 的答案, ...Climbing Stairs Easy #83 Remove Duplicates from Sorted L
Climbing Stairs 121 买卖股票的最佳时机 Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖...
Climbing Stairs 01. two sum 经典例题: 1. 二维数组查找问题。限制条件为左到右递增,上到下递增。 2. KMP算法实现 UVA problem 100 - The 3n + 1 problem 151 - Power Crisis -> 约瑟夫问题DP 问题 10607 - ...
Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要是以下几种: 基础技巧:分治、二分、贪心 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、深度优先遍历,...
70.Climbing Stairs, 198.House Robber, 55.Jump Game 72.Edit Distance, 97.Interleaving String, 115.Distinct Subsequences 2017/04/24 (Lintcode)92.Backpack, (Lintcode)125.Backpack II, (Lintcode)564....
Climbing Stairs 0719. Find K-th Smallest Pair Distance 0714. Best Time to Buy and Sell Stock with Transaction Fee 0713. 乘积小于K的子数组 0695. 岛屿的最大面积 0674. 最长连续递增序列 0673. 最长递增子...
Leetcode扑克 ...Climbing Stairs 变态跳台阶 矩形覆盖 二进制中1的个数 191. Number of 1 Bits 数值的整数次方 50. Pow(x, n) 调整数组顺序使奇数位于偶数前面 905. Sort Array By Parity 链表中倒数第
Climbing Stairs 一开始用传统的递归解题,结果TL了。看了下Dscuss,搜索了一下,发现这道居然就是典型的动态规划题,用哈希把子问题的答案记录了就能节省大量的运行时间。 Linked List Cycle 判断链表是否有环。...
Climbing Stairs 入门级记忆化dp 2014.10.30 Merge Sorted Array 归并排序基础 Remove Duplicates from Sorted List 脑残简单题 2014.10.31 *****今天被题目ThreeSum虐出翔,打了球太累,过几天
java lru leetcode 自述文件 这是我的 leetcode 答案库。 ...Climbing Stairs (爬踏板) 力码: localAnswer-python01: localAnswer-python02: 两数之和 力码: localAnswer-java1: localAnswer-
Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]