python中没有&& ,and有短路特点
python中 a and b
a为False返回a , a为True返回b
&&逻辑与不是&按位与
temp and (temp = 1) #这在python中是语法错误and 后面不能有=,可以有+ -其他的
&&短路特点:前面为假,后面不计算
链接:https://www.nowcoder.com/questionTerminal/7a0da8fc483247ff8800059e12d7caf1
来源:牛客网
解题思路:
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)。