一、python相关知识:
1、map的用法:
map(func, list1,list2,…)
map可以将function作用到每一个list上,如:
def f(x):
return x*x
map(f, [1,2,3])
上述函数会返回[1,4,9]返回的是一个列表
注意:不一定只能作用于数值,只要列表里的变量类型与函数规定的变量类型相符则可以计算,而且map返回的是一个列表
2、对于python的list,慎用for循环,能用while就用while, for会跟丢数据的(我找不到出处啦,明天回公司再弄吧)
二、题目:Pascal‘s Triangle II
思路:利用同一个数组进行循环计算,每次循环在最后加上1,然后进行j+j-1项的计算得到最后的结果
代码:
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
i = 0
row = []
for i in range(rowIndex+1):
row.append(0)
row[0] = 1
i = 0
while i < rowIndex+1:
j = i
while j >= 1:
row[j] = row[j-1] + row[j]
j -= 1
i += 1
return row
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
i = 0
row = []
while i < rowIndex:
row.append(1)#实际上这一步得到的是第i+1项的那个数组咯
j = i
while j >= 1:
row[j] = row[j-1] + row[j]
j -= 1
i += 1
row.append(1)
return row
要注意对比两种方式的不一样的地方
还是工整吧