剑指offer 47. 求1+2+3+...+n

原题

求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

Reference Answer

思路分析

思路很清奇,采用递归思路即可。

# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return n and n+self.Sum_Solution(n-1)
                 

猜你喜欢

转载自blog.csdn.net/Dby_freedom/article/details/84452215