美文网首页
64-求 1+2+…+n

64-求 1+2+…+n

作者: 一方乌鸦 | 来源:发表于2020-05-07 09:39 被阅读0次

求 1+2+...+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

使用短路

class Solution {
    int res = 0;
    public int sumNums(int n) {
        boolean b = n > 0 && sumNums(n - 1) > 0;
        res += n;
        return res;
    }
}

相关文章

网友评论

      本文标题:64-求 1+2+…+n

      本文链接:https://www.haomeiwen.com/subject/wcfkghtx.html