【剑指Offer】 47.求1+2+3+...+n python实现

题目描述
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

要求:不能使用乘除、for、while、if、else等

方法一:使用range和sum

方法二:使用reduce

def get_sum1(n):
    return sum(range(1, n+1))
def get_sum2(n):
    return reduce(lambda x, y: x+y, range(1, n+1))
发布了116 篇原创文章 · 获赞 6 · 访问量 6168

猜你喜欢

转载自blog.csdn.net/weixin_42247922/article/details/104031727