-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpascals-triangle-ii.py
More file actions
54 lines (45 loc) · 1.45 KB
/
pascals-triangle-ii.py
File metadata and controls
54 lines (45 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pascals-triangle-ii
# https://leetcode.com/problems/pascals-triangle-ii/
# Given an index k, return the kth row of the Pascal's triangle.
#
# For example, given k = 3,
# Return [1,3,3,1].
#
# Note:
# Could you optimize your algorithm to use only O(k) extra space?
import unittest
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
nlist = []
n = rowIndex
for k in xrange(n+1):
nlist.append(self.nchoosek(n,k))
return nlist
def nchoosek(self,n,k):
if k == 0 or k == n :
return 1
else:
return self.factorial(n)/(self.factorial(k) * self.factorial(n-k))
def factorial(self,n):
if n==0:
return 1
else:
return n*self.factorial(n-1)
class TestPascalsTriangle(unittest.TestCase):
my_solution = Solution()
# def test_pascals_triangle_1(self):
# self.assertEqual(self.my_solution.getRow(1),[1])
def test_pascals_triangle_1(self):
self.assertEqual(self.my_solution.getRow(1),[1, 1])
def test_pascals_triangle_2(self):
self.assertEqual(self.my_solution.getRow(2),[1, 2, 1])
def test_pascals_triangle_3(self):
self.assertEqual(self.my_solution.getRow(3),[1, 3, 3, 1])
def test_pascals_triangle_4(self):
self.assertEqual(self.my_solution.getRow(4),[1, 4, 6, 4, 1])
if __name__ == "__main__":
unittest.main()