思路:使用遞歸f(n) = f(n-1) + n, 但是不能使用if進行遞歸出口的控制,因此利用python中and的屬性,即and判斷都為真的話輸出and后面的那個數字
# -*- coding:utf-8 -*-
# -*- coding:utf-8 -*-
class Solution:
def Sum_Solution(self, n):
# write code here
ans = (n>0) and n
return ans and self.Sum_Solution(n-1)+ans
