求1+2+3+.....+n-python

思路:使用递归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
原文地址:https://www.cnblogs.com/dolisun/p/11334629.html