def Sum_Solution(n): # write code here if n==1: return 1 else: ans=n+Sum_Solution(n-1) return ans
问题反思:1.冒号输入开始有点问题,导致没有办法运行,牛客网又不能提示错误,差错好一会 开始写代码的时候,一定要注意右下角是不是英文输入模式 (英文: () 中文:())
2.选择骚操作,使用 and 的短路求值原理,也就是如果and 前面的是FALSE,那么后面就短路了,不需要考虑
class Solution { public: int Sum_Solution(int n) { int ans = n; ans && (ans += Sum_Solution(n - 1)); return ans; } };
1hour
原文:https://www.cnblogs.com/captain-dl/p/10646795.html