題目描述
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等關鍵字及條件判斷陳述句(A?B:C),
思路
短路運算 + 遞回, 代替回圈,
時間復雜度O(n),空間復雜度O(n),
短路遞回代碼
public class Solution {
public int Sum_Solution(int n) {
if(n < 1) return 0;
boolean tmp = n > 0 && (n += Sum_Solution(n-1)) > 0;
return n;
}
}
筆記
無
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/205849.html
標籤:其他
下一篇:不用加減乘除做加法
