Java for LeetCode 070 Climbing Stairs
2015-05-16 19:32
483 查看
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?
解题思路:
递归会导致超时,用动态规划即可,JAVA实现如下;
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
解题思路:
递归会导致超时,用动态规划即可,JAVA实现如下;
public int climbStairs(int n) { if(n==1) return 1; else if(n==2) return 2; int[] v=new int ; v[0]=1; v[1]=2; for(int i=2;i<v.length;i++) v[i]=v[i-1]+v[i-2]; return v[n-1]; }
相关文章推荐
- Java for LeetCode 132 Palindrome Partitioning II
- Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree
- Java for LeetCode 101 Symmetric Tree
- LeetCode 34 — Search for a Range(C++ Java Python)
- Java for LeetCode 225 Implement Stack using Queues
- leetcode 070 —— Climbing Stairs
- Java for LeetCode 141 Linked List Cycle
- Java for LeetCode 142 Linked List Cycle II
- Java for LeetCode 112 Path Sum
- Java for LeetCode 028 Implement strStr()
- Java for LeetCode 173 Binary Search Tree Iterator
- Java for LeetCode 189 Rotate Array
- Java for LeetCode 061 Rotate List
- LeetCode 1 Two Sum for java
- Java for LeetCode 237 Delete Node in a Linked List
- Java [leetcode 34]Search for a Range
- Java for LeetCode 042 Trapping Rain Water
- Java for LeetCode 144 Binary Tree Preorder Traversal
- Java for LeetCode 108 Convert Sorted Array to Binary Search Tree
- Java for LeetCode 114 Flatten Binary Tree to Linked List