链接
https://leetcode-cn.com/problems/qiu-12n-lcof/
前言
题目
求 1+2+...+n
,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
示例1:
输入: n = 3
输出: 6
示例 2:
输入: n = 9
输出: 45
提示:
1 <= n <= 10000
关键
思路1
- python自带sort()函数
class Solution:
def sumNums(self, n: int) -> int:
def sum(n):
return n and n+sum(n-1)
return sum(n)
疑问
- and与操作
- (1)如果多个变量均非0(包括None、False等),那么返回最后一个变量的值。如3 and 2 and ‘a’的返回值为’a’;
- (2)如果多个变量中存在0值,则返回第一个0值。如1 and ‘a’ and 0 and None的返回值为0。