求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
数据范围: 
进阶: 空间复杂度
,时间复杂度 )
进阶: 空间复杂度
class Solution: def Sum_Solution(self , n: int) -> int: func_arr, offset = [lambda x:1, self.Sum_Solution], [0, n] return offset[min(n - 1, 1)] + func_arr[min(n - 1, 1)](n - 1)
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.choice = ['Solution().StopCur(n-1)', 'Solution().Sum_Solution(n-1)'] def Sum_Solution(self, n): # write code here return n + eval(self.choice[not not n]) def StopCur(self, n): return 0
class Solution: def Sum_Solution(self, n): # write code here return n and n+self.Sum_Solution(n-1)
# -*- coding:utf-8 -*- class Solution: def Sum_Solution(self, n): # write code here\ if n == 1: return 1 return self.Sum_Solution(n-1) + n #挺像阶乘的做法 反正把问题变成更小的问题 然后找到终止条件也就是n = 1的时候。