剑指offer
来源:互联网 发布:凹凸曼畅游全球网络 编辑:程序博客网 时间:2024/06/07 06:56
package Chapter2;/** * 面试题9:菲波那切数列 * 输入一个整数n,请你输出斐波那契数列的第n项。 * 1、1、2、3、5、8、13、21、34、 *//* * 变形题: * 一只青蛙一次可以跳上1级台阶,也可以跳上2级。 * 求该青蛙跳上一个n级的台阶总共有多少种跳法。 */public class _09_fibonacci {public static void main(String[] args) {_09_01_function f = new _09_01_function();System.out.println(f.fibonacci(9));}}class _09_01_function {int fibonacci(int n) {int []result = {0, 1};if(n < 2) return result[n];int data = 0;int data_first = 0;int data_second = 1;for (int i = 2; i <= n; i++) {data = data_first + data_second;data_first = data_second;data_second = data;}return data;}}
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
public class Solution { public int JumpFloorII(int n) { if (n == 1) return n; int []dp = new int[n + 1]; for (int i = 1; i <= n; i++) dp[i] = 1; for (int i = 2; i <= n; i++) for(int j = i-1; j >= 1; j--) dp[i] += dp[j]; return dp[n]; }}
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
public class Solution { public int RectCover(int n) { if (n < 3) return n; int val = 0; int first = 1; int second = 2; for (int i = 3; i <= n; i++) { val = first + second; first = second; second = val; } return val; }}
阅读全文
0 0
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 《剑指offer》
- 剑指Offer
- 剑指offer
- 剑指offer
- php中Json字符串key加引号
- redis 数据结构
- 论文查找和筛选的实用技能
- 处理三级缓存应用
- Linux下redis的安装
- 剑指offer
- 说说IO(六)- Driver & IO Channel
- HTML5+规范:gallery(管理系统相册)
- 268条PCB Layout设计规范
- token的使用及原理
- 高通Modem qmi log
- ¥¥¥¥ 63大计算机科学顶级会议
- 说说IO(七)- RAID
- 我的C++自学之路01