https://github.com/lepharamramchiary/pascals-triangle-leetcode
https://github.com/lepharamramchiary/pascals-triangle-leetcode
cpp
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lepharamramchiary/pascals-triangle-leetcode
- Owner: LepharamRamchiary
- Created: 2023-02-16T09:19:21.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-16T09:28:02.000Z (over 2 years ago)
- Last Synced: 2024-12-29T20:33:26.700Z (5 months ago)
- Topics: cpp
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
**# Pascal's Triangle**
**Question**:-
Given an integer numRows, return the first numRows of Pascal's triangle.
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```Example 1:
```
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
```
Example 2:
```
Input: numRows = 1
Output: [[1]]
```**Solution**
```c++
#include
using namespace std;class Solution
{
public:
vector> generate(int numRows)
{
vector> ans(numRows);
for (int i = 0; i < numRows; i++)
{
ans[i].resize(i + 1);
for (int j = 0; j <= i; j++)
{
if (j == 0 || j == i)
{
ans[i][j] = 1;
}
else
{
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];
}
}
}
return ans;
}
};int main()
{
Solution sb;
int n;
cout << "Enter The number that you need to rows: ";
cin >> n;
vector> rows = sb.generate(n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
cout << rows[i][j] << " ";
}
cout << endl;
}
return 0;
}