Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80ba2e9

Browse files
add python solution of 118
1 parent f201289 commit 80ba2e9

File tree

2 files changed

+47
-1
lines changed
  • codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string

2 files changed

+47
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
5656
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_54.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_54.py)|![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Introduction to 2D Array
5757
|66|[Plus One](https://leetcode.com/problems/plus-one/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_66.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_66.py)|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to Array
5858
|67|[Add Binary](https://leetcode.com/problems/add-binary/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_67.java) & Python|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to String
59-
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_118.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to 2D Array
59+
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_118.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_118.py)| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to 2D Array
6060
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_119.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Conclusion
6161
|151|[Reverse Words in a String](https://leetcode.com/problems/pascals-triangle/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_151.java) & Python| ![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Conclusion
6262
|167|[Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_167.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Two-Pointer Technique
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
118. Pascal's Triangle
3+
4+
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
5+
6+
In Pascal's triangle, each number is the sum of the two numbers directly above it.
7+
8+
Example:
9+
10+
Input: 5
11+
Output:
12+
[
13+
[1],
14+
[1,1],
15+
[1,2,1],
16+
[1,3,3,1],
17+
[1,4,6,4,1]
18+
]
19+
"""
20+
21+
22+
class Solution(object):
23+
def generate(self, numRows):
24+
"""
25+
:type numRows: int
26+
:rtype: List[List[int]]
27+
"""
28+
if numRows <= 0:
29+
return []
30+
if numRows == 1:
31+
return [[1]]
32+
33+
triangle = []
34+
i = 0
35+
while i < numRows:
36+
row = []
37+
j = 0
38+
while j < i + 1:
39+
if j == 0 or j == i:
40+
row.append(1)
41+
else:
42+
row.append(triangle[i - 1][j - 1] + triangle[i - 1][j])
43+
j += 1
44+
triangle.append(row)
45+
i += 1
46+
return triangle

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /