当前位置:网站首页>Leetcode 70:Climbing Stairs
Leetcode 70:Climbing Stairs
2022-07-19 02:42:00 【Little sun who wants to be a program yuan】
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?
Note: Given n will be a positive integer.
Example 1:
Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps
Example 2:
Input: 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step
answer :
a. recursive ( Overtime )
class Solution {
public int climbStairs(int n) {
if(n < 0)
return 0;
if(n == 0 ||n == 1)
return 1;
return climbStairs(n - 1) + climbStairs(n - 2);
}
}b. Save results
class Solution {
public int[] nums = new int[1000];
public int climbStairs(int n) {
if(n < 0)
return 0;
if(n == 0 ||n == 1)
return 1;
if(nums[n] > 0)
return nums[n];
nums[n] = climbStairs(n - 1) + climbStairs(n - 2);
return nums[n];
}
}
边栏推荐
- LAMP平台部署及应用
- Use of sqlmap
- Understanding: what is interface and the concept of interface
- Sword finger offer 53 - I. find the number I in the sorted array
- Analysis of the paradise of metauniverse developers the ecological value of the metauniverse protocol caduceus
- rsync远程同步(增量备份)
- next数组-循环节
- Cocoon breaking and rebirth of 3D NFT: caduceus decentralized edge rendering technology
- Simple use case writing specification
- uniapp微信小程序登录(先授权微信后授权手机号)-- (1)
猜你喜欢

The JMeter BeanShell implementation writes the parameterized data generated by the request to the file

SSH远程控制与访问

YUM仓库服务与PXE自动部署系统

Leetcode buckle classic topic - 82 Maximum rectangle in column chart

Decentralized edge rendering meta universe protocol cadeus was invited to attend the cbaia 2022 summit to enable more Web3 application scenarios with technology

安装软件提示无法定位程序输入点AddDllDirectory于动态链接库Kernel32.dll上(文末有下载地址)

Getting to know Alibaba cloud environment construction for the first time: unable to connect remotely, and having been in the pit: the server Ping fails, FTP is built, the server builds the database,

网络一般知识(详)

子网划分(详)
![[unity Editor Extension] find all objects of a script attached in the scene and resources](/img/c2/ea07a227535755945100dc80a43658.png)
[unity Editor Extension] find all objects of a script attached in the scene and resources
随机推荐
全链路压测
MeterSphere基于JMeter分布式性能压测平台
Subnet division (see details)
Flyway的SaaS多租户实现方案
Find() (if the name is used by too many people, I will add words)
并发虚拟用户、RPS、TPS的解读
D - parity game discretization + weighted union search set
Sigaga
For solopi app performance test
[antv G2] how to solve the memory leak caused by G2
Jstat命令查看jvm的GC情况
Network layer transmission protocol (detailed)
After unity imports the FBX model, the rotation and position of the object will change automatically at runtime
已知先序遍历中序遍历,求树的层序遍历
Flask template injection
[unity development tips] unity mixer mixer controls global volume
CTFHub----RCE
简单记录一下并查集
2022.6.28-database-1 Isolation level of database
Chapter 1 - multi agent system