题解 | 求1+2+3+...+n
class Solution {
public:
int Sum_Solution(int n) {
//递归的核心点就在于递归结束的条件:
//巧妙利用了‘&&’运算符“短路”的特性——一旦左边的表达式为0(假)就不再进行下一步
n && (n += Sum_Solution(n - 1));
return n;
}
};
class Solution {
public:
int Sum_Solution(int n) {
//递归的核心点就在于递归结束的条件:
//巧妙利用了‘&&’运算符“短路”的特性——一旦左边的表达式为0(假)就不再进行下一步
n && (n += Sum_Solution(n - 1));
return n;
}
};
相关推荐