题目传送门
题解
- 直接通项公式即可,不过不够亮点,
- 循环肯定是不允许的,如果使用递归,一般都需要if来控制边界
- 可以使用 && 的特性(如果前面false,则不判断后面)来控制递归边界
AC-Code
class Solution {
public:
int Sum_Solution(int n) {
n > 1 && (n += Sum_Solution(n-1));
return n;
}
};
微信扫一扫
题目传送门
class Solution {
public:
int Sum_Solution(int n) {
n > 1 && (n += Sum_Solution(n-1));
return n;
}
};
相关推荐