求 1+2+...+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
使用短路
class Solution {
int res = 0;
public int sumNums(int n) {
boolean b = n > 0 && sumNums(n - 1) > 0;
res += n;
return res;
}
}
求 1+2+...+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
使用短路
class Solution {
int res = 0;
public int sumNums(int n) {
boolean b = n > 0 && sumNums(n - 1) > 0;
res += n;
return res;
}
}
本文标题:64-求 1+2+…+n
本文链接:https://www.haomeiwen.com/subject/wcfkghtx.html
网友评论