方法一 import functools sum = 0 for i in range(1,21): sum = sum + functools.reduce(lambda x,y: x*y, range(1, i+1)) print(sum) lambda x,y: x*y 的意思是:有一个函数,接受x, y两个参数做乘法. import functools 方法二 def f(n): if n == 1: return 1 else:
计算阶层 普通方法: -使用循环 #!/usr/bin/python def factorial(n): sum = 1 for i in range(1,n+1): sum *= i return sum print factorial(5) 计算阶层 python 7.py 120 #!/usr/bin/python def factorial(n): sum = 0 for i in range(1
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. Example 1: Input
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 日期 题目地址:https://leetcode.com/problems/robot-return-to-origin/description/ 题目描述 There is a robot starting at position (0, 0), the origin, on a 2D plan
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple time
python中,无论是对的list求和还是求积,我都给出了两种方法. 1.对list求和 1.1 s=0 for i in range(10): s+=i 1.2 s=sum(range(10)) 2 .对list求积 2.1 s=0 for i in range(1,10): s*=i 2.2 from operator import mul reduce(mul,range(1,10)