循环相加
public class Solution { public int Sum_Solution(int n) { int sum = n; for(int i = 1; i < n; i++){ sum+=i; } return sum; } }
递归
public class Solution { public int Sum_Solution(int n) { if(n == 1) return n; return n+Sum_Solution(n-1); } }
变形递归
public int Sum_Solution(int n) { int sum = n; boolean result = (n > 0) && ((sum += Sum_Solution(n-1)) > 0); return sum; }