题目
思路
- 法1.采用递归,利用逻辑与的短路特性
- 法2. 调用Java库函数
代码
链接:https://www.nowcoder.com/questionTerminal/7a0da8fc483247ff8800059e12d7caf1
来源:牛客网
public class Solution {
int Sum_Solution(int n) {
int sum = n;
boolean temp = (sum!=0) && ((sum += Sum_Solution(n-1))!=0);
return sum;
}
}
public class Solution {
public int Sum_Solution(int n) {
int sum = (int) (Math.pow(n,2) + n);
return sum>>1;
}
}