64.求1+2+...+n
小于 1 分钟
64.求1+2+...+n
参考链接
剑指 Offer 64. 求1+2+…+n-跟着帅地玩转校招,刷爆各类算法题帅地玩Offer
LCR 189. 设计机械累加器 - 力扣(LeetCode)
优秀题解
class Solution {
public int mechanicalAccumulator(int target) {
boolean x = target > 1 && (target += mechanicalAccumulator(target - 1)) > 0;
return target;
}
}
作者:Krahets
链接:https://leetcode.cn/problems/qiu-12n-lcof/solutions/208315/mian-shi-ti-64-qiu-1-2-nluo-ji-fu-duan-lu-qing-xi-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
采用 逻辑运算符 代替 递归终止条件的判断,妙不可绝!