LeetCode开心刷题五十一天——118. Pascal's Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal's Triangle II

118. Pascal's Triangle
Easy

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

BONUS:

1.python用少量的数据类型实现了很多的功效,主要是:[列表] (元组){字典}

0x02. 列表(List)

List(列表) 是 Python 中使用最频繁的数据类型。

列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。

操作实例:  

1
2
3
4
5
6
7
8
9
list  =   [ 'apple' 'jack' 798 2.22 36 ]
otherlist  =  [ 123 'xiaohong' ]
 
print ( list )                              #输出完整列表
print ( list [ 0 ])                           #输出列表第一个元素
print ( list [ 1 : 3 ])                         #输出列表第二个至第三个元素
print ( list [ 2 :])                          #输出列表第三个开始至末尾的所有元素
print (otherlist  *  2 )                     #输出列表两次
print ( list  +  otherlist)                  #输出拼接列表

 

0x03. 元祖(Tuple)

元组是另一个数据类型,类似于List(列表)。

元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。

操作实例与列表相似

 

0x04. 字典(Dictionary)

字典(dictionary)是除列表以外Python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。

两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。

字典用"{ }"标识。字典由索引(key)和它对应的值value组成。

操作实例:

1
2
3
4
5
6
7
8
9
10
dict  =  {}
dict [ 'one' =  'This is one'
dict [ 2 =  'This is two'
tinydict  =  { 'name' : 'john' , 'code' : 5762 , 'dept' : 'sales' }
 
print ( dict [ 'one' ])                           #输出键为'one'的值
print ( dict [ 2 ])                               #输出键为2的值
print (tinydict)                              #输出完整的字典
print (tinydict.keys())                       #输出所有键
print (tinydict.values())                     #输出所有值

2、lambda表达式(不知道跳转表有啥用)

常用来编写跳转表(jump table),就是行为的列表或字典。例如:

 

L = [(lambda x: x**2),  

    (lambda x: x**3),  

    (lambda x: x**4)]  

print L[0](2),L[1](2),L[2](2)  

  

D = {'f1':(lambda: 2+3),  

    'f2':(lambda: 2*3),  

    'f3':(lambda: 2**3)}  

print D['f1'](),D['f2'](),D['f3']() 

 

结果:

4 8 16  

5 6 8

0x02. 列表(List)

List(列表) 是 Python 中使用最频繁的数据类型。

列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。

操作实例:  

1
2
3
4
5
6
7
8
9
list  =   [ 'apple' 'jack' 798 2.22 36 ]
otherlist  =  [ 123 'xiaohong' ]
 
print ( list )                              #输出完整列表
print ( list [ 0 ])                           #输出列表第一个元素
print ( list [ 1 : 3 ])                         #输出列表第二个至第三个元素
print ( list [ 2 :])                          #输出列表第三个开始至末尾的所有元素
print (otherlist  *  2 )                     #输出列表两次
print ( list  +  otherlist)                  #输出拼接列表

 

0x03. 元祖(Tuple)

元组是另一个数据类型,类似于List(列表)。

元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。

操作实例与列表相似

 

0x04. 字典(Dictionary)

字典(dictionary)是除列表以外Python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。

两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。

字典用"{ }"标识。字典由索引(key)和它对应的值value组成。

操作实例:

1
2
3
4
5
6
7
8
9
10
dict  =  {}
dict [ 'one' =  'This is one'
dict [ 2 =  'This is two'
tinydict  =  { 'name' : 'john' , 'code' : 5762 , 'dept' : 'sales' }
 
print ( dict [ 'one' ])                           #输出键为'one'的值
print ( dict [ 2 ])                               #输出键为2的值
print (tinydict)                              #输出完整的字典
print (tinydict.keys())                       #输出所有键
print (tinydict.values())                     #输出所有值
class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        res=[[1]]
        for i in range(numRows):
            res+=[list(map(lambda x,y:x+y,[0]+res[-1],res[-1]+[0]))]
        return res[:numRows]

Solu=Solution()
# nums=list(map(int, input().split()))
# numRows=int(input())
# nums=[2, 7, 11, 15]
# target=9
print(Solu.generate(numRows=5))
119. Pascal's Triangle II
Easy

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 3
Output: [1,3,3,1]

Follow up:

Could you optimize your algorithm to use only O(k) extra space?

这个和上面的题只要把最后返回的改下就好,不过应该有更好的解法:

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        res=[[1]]
        for i in range(rowIndex):
            res+=[list(map(lambda x,y:x+y,[0]+res[-1],res[-1]+[0]))]
        return res[rowIndex]

猜你喜欢

转载自www.cnblogs.com/Marigolci/p/11695130.html