增加面试题64. 求1+2+…+n

This commit is contained in:
LingZhaoHui 2020-06-02 22:48:27 +08:00
parent b2f710e5b4
commit bb8f35cfef

20
src/num/SumNums.java Normal file
View File

@ -0,0 +1,20 @@
package num;
/**
* @file SumNums.java
* @apiNote 1+2+...+n 要求不能使用乘除法forwhileifelseswitchcase等关键字及条件判断语句A?B:C
* https://leetcode-cn.com/problems/qiu-12n-lcof/
* @author zeekling
* @version
* @date 2020-06-02
*/
public class SumNums{
public int sumNums(int n) {
boolean res = n > 0 && (n += sumNums(n-1)) > 0;
return n;
}
}