304 North Cardinal St.
Dorchester Center, MA 02124

# Pascal’s Triangle LeetCode Solution

## Problem – Pascal’s Triangle

Given an integer `numRows`, return the first numRows of Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

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

Example 2:

``````Input: numRows = 1
Output: []``````

Constraints:

• `1 <= numRows <= 30`

### Pascal’s Triangle LeetCode Solution in C++

``````class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int>> r(numRows);

for (int i = 0; i < numRows; i++) {
r[i].resize(i + 1);
r[i] = r[i][i] = 1;

for (int j = 1; j < i; j++)
r[i][j] = r[i - 1][j - 1] + r[i - 1][j];
}

return r;
}
};
``````

### Pascal’s Triangle LeetCode Solution in Python

``````def generate(self, numRows):
res = []
for i in range(1, numRows):
res += [map(lambda x, y: x+y, res[-1] + ,  + res[-1])]
return res[:numRows]
``````

### Pascal’s Triangle LeetCode Solution in Java

``````public class Solution {
public List<List<Integer>> generate(int numRows)
{
List<List<Integer>> allrows = new ArrayList<List<Integer>>();
ArrayList<Integer> row = new ArrayList<Integer>();
for(int i=0;i<numRows;i++)
{
for(int j=1;j<row.size()-1;j++)
row.set(j, row.get(j)+row.get(j+1));
}
return allrows;

}
``````
##### Pascal’s Triangle LeetCode Solution Review:

In our experience, we suggest you solve this Pascal’s Triangle LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Pascal’s Triangle LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Pascal’s Triangle LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions