首页 > 试题广场 >

求1+2+3+...+n

[编程题]求1+2+3+...+n
  • 热度指数:370560 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

数据范围:
进阶: 空间复杂度 ,时间复杂度
示例1

输入

5

输出

15
示例2

输入

1

输出

1
推荐
class Solution {
public:
    int Sum_Solution(int n) {
        int ans = n;
        ans && (ans += Sum_Solution(n - 1));
        return ans;
    }
};

编辑于 2015-06-19 17:53:23 回复(107)
你们绝壁猜不到我用了什么手段来处理。3秒中揭露答案。









我用了java里面的异常来处理。
public int Sum_Solution(int n) {
        int[] excep = new int[n];
        int index = 1;
        return add(excep, index);
    }

    public int add(int[] excep, int index) {
        int sum = index;
        try {
            excep[index] = 0;
            sum += add(excep, index + 1);
        } catch (Exception e) {

        }
        return sum;
    }


编辑于 2023-12-10 16:18:44 回复(0)
public class Solution {
    public int Sum_Solution(int n) {
        if(n == 1) return 1;
        return n + Sum_Solution(n-1);
    }
}

发表于 2023-06-15 14:14:07 回复(0)
import java.util.stream.LongStream;
public class Solution {
    public int Sum_Solution(int n) {
        long sum = LongStream.rangeClosed(0L, n).parallel().reduce(0, Long::sum);
        return Integer.valueOf(String.valueOf(sum)) ;
    }
}

发表于 2023-03-22 23:48:09 回复(0)
public class Solution {
    public int Sum_Solution(int n) {
        int sumNum = n;
        boolean a = (n > 0) && ((sumNum = sumNum + Sum_Solution(n - 1)) > 0);

        return sumNum;
    }
}

发表于 2023-03-09 14:14:21 回复(0)
高中还是初中的公式    1+2+3+...(n-1)+n = n * (n + 1) / 2;
public int Sum_Solution(int n) {
        return n * (n + 1) / 2;
}


发表于 2023-02-07 16:27:59 回复(0)
1、如果n的值足够大,可以使用parallel并行流方式。但此题中n<=200,不适用。
public int Sum_Solution(int n) {
  return IntStream.rangeClosed(1, n).reduce(0, Integer::sum);
}

2、使用 && 短路原则 和 递归方式:当&&左侧的表达式为false时,就不会继续执行右侧的表达式了,否则就继续执行右侧的表达式,递归将方法压入栈中,直到条件不符合后,将栈中的方法弹出并计算。
public int Sum_Solution(int n) {
  boolean flag = (n > 1) && ((n += Sum_Solution(n - 1)) < 0);
  return n;
}





发表于 2022-08-25 19:45:07 回复(0)
空间复杂度 O(1)O(1) ,时间复杂度 O(n)O(n),这个版本的答案是啥呢,递归空间复杂度不是O(n)吗
发表于 2022-05-09 10:15:16 回复(0)
public class Solution {
    public int Sum_Solution(int n) {
        n = n == 1 ? 1 : n + Sum_Solution(n-1);
        return n;
    }
}

发表于 2022-01-09 22:51:53 回复(0)
public class Solution {
    
    int ret = 0;
    public int Sum_Solution(int n) {
        // 用递归,短路作为终止条件
        
        // x没有任何含义,包括Sum_Solution(n-1)>0也是,整个式子纯粹是为了让递归执行下去并设置终止(短路)条件
        boolean x = n>0 && Sum_Solution(n-1)>0;
        // 每次执行都加上当前n,到最后一次执行就是+0
        ret += n;
        return ret;
    }
}

发表于 2022-01-09 16:23:27 回复(0)

 public int Sum_Solution(int n) {
        //等差数列求前N项和
         return n+((n*(n-1))/2);
    }

发表于 2021-10-27 18:13:23 回复(2)
public class Solution {
    public int Sum_Solution(int n) {
        return n == 1 ? 1 : Sum_Solution(n - 1) + n;
    }
}

发表于 2021-10-09 15:59:57 回复(1)
/*
用递归。
    
声明sum与n分开,避免混乱。
用boolean类型的中间变量,使用&&的短路规则,进行递归结束。
*/
    /*
    用递归。
    
    声明sum与n分开,避免混乱。
    用boolean类型的中间变量,使用&&的短路规则,进行递归结束。
    */
    public int Sum_Solution(int n) {
        //声明sum与n分开,避免混乱
        int sum = n;
        //用boolean类型的中间变量,使用&&的短路规则,进行递归结束
        boolean flag = (n > 0) && ((sum += Sum_Solution(n-1)) > 1);
        return sum;
    }


发表于 2021-08-31 17:57:12 回复(1)
    public int Sum_Solution(int n) {
        return n + n * (n-1) / 2;
    }

发表于 2021-08-24 17:55:15 回复(0)
public class Solution {
    public int Sum_Solution(int n) {
        
        if(n <= 0){
            return 0;
        }
        
        if(n == 1){
            return 1;
        }
        
        return n += Sum_Solution(n-1);
    }
}

发表于 2021-08-17 11:26:47 回复(0)
public class Solution {
    public int Sum_Solution(int n) {
        int sum = n;
        boolean flag = (n == 1) || (sum += Sum_Solution(n-1)) > 0;
        return sum;
    }
}

发表于 2021-08-06 20:41:50 回复(0)
public class Solution {
    public int Sum_Solution(int n) {
        //求和公式
        return (1+n)*n/2;
    }
}

发表于 2021-07-31 21:10:34 回复(1)
    public int Sum_Solution(int n) {
        if(n<=1)
            return n;
        int a = n + Sum_Solution(n-1);
        return a;
    }
发表于 2021-04-17 21:21:48 回复(0)

问题信息

难度:
136条回答 130150浏览

热门推荐

通过挑战的用户

查看代码