前两天面试遇到的一个题,当时没有想清楚,今天想了一下,po出来: # -*-encoding:utf-8-*- import sys end = 0 # 终点 cnt = 0 # 统计组合方式 def jump(start): global cnt for i in [1,2]: cur = str(start)+"+"+str(i) if eval(cur) >= end: print cur cnt += 1 continue jump(cur) def main(n): &
递归与循环 递归:在一个函数的内部调用这个函数. 本质:把一个问题分解为两个,或者多个小问题(多个小问题相互重叠的部分,会存在重复的计算) 优点:简洁,易于实现. 缺点:时间和空间消耗严重,如果递归调用的层级太多,就会超出栈容量. 循环:通过设置计算的初始值及终止条件,在一个范围内重复运算. 斐波拉契数列 题目一:写一个函数,输入n,求斐波拉契(Fibonacci)数列的第n项,定义如下: 第一种解法:用递归的算法: long long Fabonacci(unsigned int n) { i
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Input: [2,3,1,1,