描述:
有一楼梯共n级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第n级,共有多少种走法? 现在给你一个正整数n(0<n<40 n="2,则输出1(你只有一种走法,走一步,从第一级到第二级)Python代码:
水题 竟然是斐波那契数列问题
l = [1,1] for i in range(2,n): l.append((l[i-1] + l[i-2])) print(l[n-1])
我他妈写了个二叉树运行超时
# 二叉树******************************* start # from __future__ import print_function # import math # import copy # n=1 # # # L = [] # # # class TreeNode(object): # def __init__(self,data,left,right): # self.data = data # self.left = left # self.right = right # # def add_tree_node(tn): # n = tn.data['reminder'] # if n > 0 and tn.right == None: # #右子孙 # temp = TreeNode({'reminder':n-1,'node_num':1}, None, None) # tn.right = temp # add_tree_node(temp) # if n > 1 and tn.left == None: # #左子孙 # temp = TreeNode({'reminder':n-2,'node_num':2},None,None) # tn.left = temp # add_tree_node(temp) # elif n == 1 and tn.left == None: # #左子孙 # temp = TreeNode({'reminder':n-2,'node_num':-1},None,None) # tn.left = temp # add_tree_node(temp) # return # # # # # def travel_tree(tn,l): # l.append(tn.data['node_num']) # # print(tn.data['node_num']) # if tn.left == None and tn.right == None and tn.data['node_num'] != -1: # # L.append(l) # else: # if tn.right!=None: # travel_tree(tn.right, copy.deepcopy(l)) # if tn.left != None: # travel_tree(tn.left,copy.deepcopy(l)) # l.pop() # return # # def count_method(n): # if n > 0: # root = TreeNode({'reminder':n,'node_num':0},None,None) # add_tree_node(root) # l = [] # travel_tree(root,copy.deepcopy(l)) # # print(L) # print(len(L),end='') # count_method(n) # 二叉树******************************* end