求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
数据范围:
进阶: 空间复杂度 ,时间复杂度
进阶: 空间复杂度 ,时间复杂度
public static int Sum_Solution(int n) { int sum = n; boolean flag = (sum > 0) && ((sum += Sum_Solution(--n)) > 0); return sum; }
public static int Sum_Solution1(int n) { return (int) (Math.pow(n, 2) + n) >> 1; }
public static int Sum_Solution2(int n) {
int res = 0;
int a = n;//若a=2=10
int b = n + 1;//b=3=11
while (a != 0) {
if ((a & 1) == 1)//a在第二位==1的时候才更新res=0+110=6
res += b;
a >>= 1;//a右移1位 1
b <<= 1;//b左移动1位 110
}
return res>>=1;//n(n+1)/2 }
public int Sum(int n) { int res = multi(n, n + 1);//n*(n-1) return res>>=1;//n*(n-1)/2 } private int multi(int a, int b) { int res = 0; //循环体内部, if ((a & 1) == 1), res += b; boolean flag1 = ((a & 1) == 1) && (res += b) > 0; a >>= 1; b <<= 1; // while (a != 0) {}循环条件 boolean flag2 = (a != 0) && (res += multi(a,b)) > 0 ; return res; }
temp[] test = new temp[n]
public class Solution { public int Sum_Solution(int n) { int result = 0; int a = 1; boolean value = ((n!=0) && a == (result = Sum_Solution(n-1))); result += n; return result; } }
# -*- coding:utf-8 -*- class Solution: def Sum_Solution(self, n): return n and (n + self.Sum_Solution(n - 1))
1. import java.util.*; public class Solution { public int Sum_Solution(int n) { int sum = n; boolean ans = (n>0)&&((sum+=Sum_Solution(n-1))>0); return sum; } } 2. import java.util.*; public class Solution { public int Sum_Solution(int n) { n = (int)(Math.pow(n,2)+n)>>1; return n; } }
解题的关键是使用递归,利用递归代替了循环,并且使用逻辑与运算判断n何时为0 class Solution: def __init__(self): self.sum = 0 def Sum_Solution(self, n): # write code here def recur(n): self.sum += n n -= 1 return (n>0) and self.Sum_Solution(n) recur(n) return self.sum 函数recur()实现了循环,从n一直递减加到了1,逻辑与and操作实现了当n=0时,不再计算Sum_Solution(n), 返回self.sum
先取对数再指数算回去。就不用傻傻的递归。
class Solution {
public:
int Sum_Solution(int n) {
//return n*(n+1)/2;
return multi(n, n + 1) >> 1;
}
int multi(int a, int b){
// we can guarantee that both a and b is positive integers
int res = int(pow(10, log10(a) + log10(b))+0.5);
return res;
}
};
解题思路: 1.需利用逻辑与的短路特性实现递归终止。 2.当n==0时,(n>0)&&((sum+=Sum_Solution(n-1))>0)只执行前面的判断,为false,然后直接返回0; 3.当n>0时,执行sum+=Sum_Solution(n-1),实现递归计算Sum_Solution(n)。 public int Sum_Solution(int n) { int sum = n; boolean ans = (n>0)&&((sum+=Sum_Solution(n-1))>0); return sum; }
public class Solution { public int Sum_Solution(int n) { n = n == 1 ? 1 : n + Sum_Solution(n-1); return n; } }
public class Solution { public int Sum_Solution(int n) { //数学对数原理: // log(a)+log(b) = log(a*b) //log(a)-logb = log(a/b) //所以log(ret)= log((1+n)*n/2) //通过等号两边同时去掉对数取得ret,并消除误差即可 //假设log(a,N)表示底数为a,真数为N的对数,则a^log(a,N) = N //所以ret = 10^log(10,N) = 10^log10(N) = N double a = Math.log10(1+n) + Math.log10(n); double b = Math.log10(2); int ret = (int)(Math.pow(10,a-b)+0.5);//可通过四舍五入消除误差 return ret; } }
静态变量C++
class Temp { public: static int sum; static int s; Temp() { ++s; sum += s; } }; int Temp::sum = 0; int Temp::s = 0; class Solution { public: int Sum_Solution(int n) { vector<Temp> tmp(n); int res = Temp::sum; Temp::sum = 0; Temp::s = 0; return res; } };
class Solution { public: int Sum_Solution(int n) { bool res[n][n+1]; return sizeof(res) >> 1; } };
/* 用递归。 声明sum与n分开,避免混乱。 用boolean类型的中间变量,使用&&的短路规则,进行递归结束。 */ public int Sum_Solution(int n) { //声明sum与n分开,避免混乱 int sum = n; //用boolean类型的中间变量,使用&&的短路规则,进行递归结束 boolean flag = (n > 0) && ((sum += Sum_Solution(n-1)) > 1); return sum; }