leetcode第70题-Climbing Stairsd

题目的意思是有n个台阶,每次只能上1或2个台阶,求出总共有几种上台阶的方法。

分析:因为每次都只能+1或+2,最后的每一个n就是由1或2的组合组成。但是换一种思路, 我们对比一些斐波那契数列,1、2、3、5、8、、、、,即f(n)=f(n-1)+f(n-2)。如果第一步走了1个台阶,剩下的组合是f(n-1),如果第一步走2个台阶,则剩下的组合f(n-2),从而得到递推式f(n)=f(n-1)+f(n-2),所以,这个爬楼梯就是一个递归的斐波那契数列问题。

剩下的问题就是如何求出斐波那契数列的问题了,一开始我使用了递归,结果超时,只能用一个数组模拟数列了。整个实现的代码如下:

#include<stdio.h>
#include<stdlib.h>

int climbStairs(int n)
{
	int a[1000];
	a[1]=1,a[2]=2;
	for(int i=3;i<1000;i++)
		a[i]=a[i-1]+a[i-2];
	return a[n];
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int len=climbStairs(n);
		printf("%d\n",len);
	}
	return 0;
}
时间: 2024-09-30 09:33:23

leetcode第70题-Climbing Stairsd的相关文章

[LeetCode] 系统刷题5_Dynamic Programming

Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题目实际上是类似于Divide and conquer或者说是DFS,但是在计算过程中有很多重复计算同样的过程的话,那么就可以用Dynamic prgramming/记忆化搜索来完成.基本就是利用空间来简化时间复杂度的过程. 可以/很有可能使用Dynamic programming的条件,满足之一即可. 1.

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目很简单,

LeetCode第四题,Add Two Numbers

题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6