0
点赞
收藏
分享

微信扫一扫

【LeetCode】剑指 Offer 64. 求1+2+…+n

追梦人的自留地 2022-01-06 阅读 22
LeetCode

【LeetCode】剑指 Offer 64. 求1+2+…+n

文章目录

在这里插入图片描述

package offer;

public class Solution64 {
    public static void main(String[] args) {
        int n = 9;
        Solution64 solution = new Solution64();
        System.out.println(solution.method(n));
    }

    private int method(int n){
        boolean x = (n > 1) && (n += method(n - 1)) > 0;
        return n;
    }
}

//时间复杂度为 O(n)
//空间复杂度为 O(n)
举报

相关推荐

0 条评论