Skip to content

Commit dbbdea2

Browse files
Solve Leetcode
- 118. Pascal's Triangle.
1 parent 0089780 commit dbbdea2

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
`118` `Easy`
2+
3+
# Pascal's Triangle
4+
5+
Given an integer numRows, return the first numRows of Pascal's triangle.
6+
7+
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
8+
9+
!["Pascal Triangle"](img.gif "Pascal Triangle")
10+
11+
## Example
12+
13+
```
14+
Input: numRows = 5
15+
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
16+
```
17+
18+
## Example
19+
20+
```
21+
Input: numRows = 1
22+
Output: [[1]]
23+
```
24+
25+
## Constraints:
26+
27+
- 1 <= numRows <= 30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public List<List<Integer>> generate(int numRows) {
3+
List<List<Integer>> answer = new ArrayList<List<Integer>>();
4+
List<Integer> row, pre= null;
5+
for(int i =0;i<numRows;i++){
6+
row = new ArrayList<Integer>();
7+
for(int j=0;j<=i;j++){
8+
if(j==0 || j==i){
9+
row.add(1);
10+
}else{
11+
// System.out.println(Arrays.toString(pre.toArray()));
12+
row.add(pre.get(j-1)+pre.get(j));
13+
}
14+
}
15+
pre = row;
16+
answer.add(row);
17+
}
18+
return answer;
19+
}
20+
}
28.2 KB
Loading

LeetCode/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# AlgoCademy
2+
3+
## Easy
4+
5+
6+
- 136 Single Number
7+
- 344 Reverse String
8+
- 20 Valid Parentheses
9+
- 118 Pascal's Triangle
10+
11+
## Medium
12+
- 73 Set Matrix Zero

0 commit comments

Comments
 (0)