2017-04-05 63 views
-2

這是leetcode上的問題。 你正在爬樓梯的情況。需要n個步驟才能到達頂端。TypeError:range()整數結束參數預期,得到浮點數

每次你可以爬1或2步。有多少種不同的方式可以爬到頂端?

注意:給定n將是一個正整數。 它爲什麼表明這一點?

展示公司標籤 顯示標籤

class Solution(object): 
    def climbStairs(self, n): 

     twostairtimes = math.floor(n/2) 
     result = 0 
     twostairresult = 0 

     while (twostairtimes>=0): 
      onestairtimes = n - (2 * twostairtimes) 
      if onestairtimes == 0: 
       result+=1 
      elif twostairtimes ==0: 
       result+=1 
      else: 
       result += self.jiecheng(n)/(self.jiecheng(twostairtimes)*self.jiecheng(onestairtimes)) 
       result += 1 
      twostairtimes=twostairtimes-1 

     return result 

    def jiecheng(self,n): 
     c = 1 
     for i in range(n+1): 
      c *= i 
     return c 

回答

0

如果您確信ninteger投這樣的:

range(int(n+1))

0

變化twostairtimes = math.floor(n/2)twostairtimes = n//2或升級到
的Python 3 CG問題解決了

相關問題