# Maybe shortest c++ solution

• ``````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][0] = 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;
}
};``````

• easy and neat code is the best explanation,isn't it?

• Thanks for sharing.
My approach is a bit different, which is also short.
https://leetcode.com/discuss/94262/short-c-solution-using-only-o-k-extra-space

• Hi,basically both of your answer based on the same logic,it's really good that you can solve it in a more efficient way.I admire that :)

• nice code Thanks for sharing

• What does r look like when numRows is 0?

• @sean46 It will be empty when the numRows is 0.

• r[i].resize(i + 1); //What does this step do here?

• Nice ! Thanks!
Python version:

``````class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
all_rows = [None] * numRows
for i in range(numRows):
all_rows[i] = [None] * (i+1)
all_rows[i][0] = all_rows[i][i] = 1
for j in range(1, i):
all_rows[i][j] = all_rows[i-1][j-1] + all_rows[i-1][j]
return all_rows
``````

• @abhishek.bailore94 how many elements in `row i`

r[i].resize(i + 1); //What does this step do here?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.