leetcode 刷题-剑指offer-64题

题目[1]

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

示例 1:

输入: n = 3
输出: 6

示例 2:

输入: n = 9
输出: 45

解答

新手上路,才学疏浅,望斧正

public class Solution19_1 {
    public int sumNums(int n) {
        try {
            int a=1/n;
            return sumNums(n-1)+n;
        }catch (Exception e){
            return 0;
        }
    }
}

  1. https://leetcode-cn.com/problems/qiu-12n-lcof/ ↩︎

posted @ 2022-01-25 11:30  发呆鱼  阅读(26)  评论(0编辑  收藏  举报