思路:使用递归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

相关文章:

  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
猜你喜欢
  • 2021-08-02
  • 2021-07-07
  • 2021-09-11
相关资源
相似解决方案