码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_70题——Climbing Stairs(简单DP题)

时间:2015-06-05 10:08:35      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

Climbing Stairs

 Total Accepted: 54579 Total Submissions: 158996My Submissions

 

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?

 

Hide Tags
 Dynamic Programming
Have you met this question in a real interview? 
Yes
 
No
 

Discuss

#include<iostream>
#include<vector>
using namespace std;
int climbStairs(int n) {
	if(n==0||n==1)
		return 1;
	int *ptr=new int[n+1];
	for(int i=0;i<n+1;i++)
		ptr[i]=0;
	ptr[n]=1;
	ptr[n-1]=1;
	for(int i=n-2;i>=0;i--)
		ptr[i]=ptr[i+1]+ptr[i+2];
	int last=ptr[0];
	delete []ptr;
	return last;
}
int main()
{
	cout<<climbStairs(3)<<endl;
}

  

leetcode_70题——Climbing Stairs(简单DP题)

标签:

原文地址:http://www.cnblogs.com/yanliang12138/p/4553793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!