Skip to content

feat: add solutions to lc problem: No.0118 #4608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions solution/0100-0199/0118.Pascal's Triangle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ tags:

我们先创建一个答案数组 $f$,然后将 $f$ 的第一行元素设为 $[1]$。接下来,我们从第二行开始,每一行的开头和结尾元素都是 $1$,其它 $f[i][j] = f[i - 1][j - 1] + f[i - 1][j]$。

时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是行数
时间复杂度 $O(n^2)$,其中 $n$ 为给定的行数。忽略答案的空间消耗,空间复杂度 $O(1)$

<!-- tabs:start -->

Expand All @@ -83,8 +83,8 @@ class Solution {
for (int i = 0; i < numRows - 1; ++i) {
List<Integer> g = new ArrayList<>();
g.add(1);
for (int j = 0; j < f.get(i).size() - 1; ++j) {
g.add(f.get(i).get(j) + f.get(i).get(j + 1));
for (int j = 1; j < f.get(i).size(); ++j) {
g.add(f.get(i).get(j - 1) + f.get(i).get(j));
}
g.add(1);
f.add(g);
Expand All @@ -105,8 +105,8 @@ public:
for (int i = 0; i < numRows - 1; ++i) {
vector<int> g;
g.push_back(1);
for (int j = 0; j < f[i].size() - 1; ++j) {
g.push_back(f[i][j] + f[i][j + 1]);
for (int j = 1; j < f[i].size(); ++j) {
g.push_back(f[i][j - 1] + f[i][j]);
}
g.push_back(1);
f.push_back(g);
Expand All @@ -123,8 +123,8 @@ func generate(numRows int) [][]int {
f := [][]int{[]int{1}}
for i := 0; i < numRows-1; i++ {
g := []int{1}
for j := 0; j < len(f[i])-1; j++ {
g = append(g, f[i][j]+f[i][j+1])
for j := 1; j < len(f[i]); j++ {
g = append(g, f[i][j-1]+f[i][j])
}
g = append(g, 1)
f = append(f, g)
Expand All @@ -140,8 +140,8 @@ function generate(numRows: number): number[][] {
const f: number[][] = [[1]];
for (let i = 0; i < numRows - 1; ++i) {
const g: number[] = [1];
for (let j = 0; j < f[i].length - 1; ++j) {
g.push(f[i][j] + f[i][j + 1]);
for (let j = 1; j < f[i].length; ++j) {
g.push(f[i][j - 1] + f[i][j]);
}
g.push(1);
f.push(g);
Expand All @@ -154,21 +154,17 @@ function generate(numRows: number): number[][] {

```rust
impl Solution {
#[allow(dead_code)]
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
let mut ret_vec: Vec<Vec<i32>> = Vec::new();
let mut cur_vec: Vec<i32> = Vec::new();

for i in 0..num_rows as usize {
let mut new_vec: Vec<i32> = vec![1; i + 1];
for j in 1..i {
new_vec[j] = cur_vec[j - 1] + cur_vec[j];
let mut f = vec![vec![1]];
for i in 1..num_rows {
let mut g = vec![1];
for j in 1..f[i as usize - 1].len() {
g.push(f[i as usize - 1][j - 1] + f[i as usize - 1][j]);
}
cur_vec = new_vec.clone();
ret_vec.push(new_vec);
g.push(1);
f.push(g);
}

ret_vec
f
}
}
```
Expand All @@ -184,8 +180,8 @@ var generate = function (numRows) {
const f = [[1]];
for (let i = 0; i < numRows - 1; ++i) {
const g = [1];
for (let j = 0; j < f[i].length - 1; ++j) {
g.push(f[i][j] + f[i][j + 1]);
for (let j = 1; j < f[i].length; ++j) {
g.push(f[i][j - 1] + f[i][j]);
}
g.push(1);
f.push(g);
Expand All @@ -194,6 +190,25 @@ var generate = function (numRows) {
};
```

#### C#

```cs
public class Solution {
public IList<IList<int>> Generate(int numRows) {
var f = new List<IList<int>> { new List<int> { 1 } };
for (int i = 1; i < numRows; ++i) {
var g = new List<int> { 1 };
for (int j = 1; j < f[i - 1].Count; ++j) {
g.Add(f[i - 1][j - 1] + f[i - 1][j]);
}
g.Add(1);
f.Add(g);
}
return f;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
63 changes: 39 additions & 24 deletions solution/0100-0199/0118.Pascal's Triangle/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ tags:

### Solution 1: Simulation

First, we create an answer array $f$, and then set the first row of $f$ to $[1]$. Next, starting from the second row, the first and last elements of each row are $1$, and the other elements are calculated by $f[i][j] = f[i - 1][j - 1] + f[i - 1][j]$.
We first create an answer array $f$, then set the first row of $f$ to $[1]$. Next, starting from the second row, the first and last elements of each row are $1$, and for other elements $f[i][j] = f[i - 1][j - 1] + f[i - 1][j]$.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of rows.
The time complexity is $O(n^2)$, where $n$ is the given number of rows. Ignoring the space consumption of the answer, the space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -72,8 +72,8 @@ class Solution {
for (int i = 0; i < numRows - 1; ++i) {
List<Integer> g = new ArrayList<>();
g.add(1);
for (int j = 0; j < f.get(i).size() - 1; ++j) {
g.add(f.get(i).get(j) + f.get(i).get(j + 1));
for (int j = 1; j < f.get(i).size(); ++j) {
g.add(f.get(i).get(j - 1) + f.get(i).get(j));
}
g.add(1);
f.add(g);
Expand All @@ -94,8 +94,8 @@ public:
for (int i = 0; i < numRows - 1; ++i) {
vector<int> g;
g.push_back(1);
for (int j = 0; j < f[i].size() - 1; ++j) {
g.push_back(f[i][j] + f[i][j + 1]);
for (int j = 1; j < f[i].size(); ++j) {
g.push_back(f[i][j - 1] + f[i][j]);
}
g.push_back(1);
f.push_back(g);
Expand All @@ -112,8 +112,8 @@ func generate(numRows int) [][]int {
f := [][]int{[]int{1}}
for i := 0; i < numRows-1; i++ {
g := []int{1}
for j := 0; j < len(f[i])-1; j++ {
g = append(g, f[i][j]+f[i][j+1])
for j := 1; j < len(f[i]); j++ {
g = append(g, f[i][j-1]+f[i][j])
}
g = append(g, 1)
f = append(f, g)
Expand All @@ -129,8 +129,8 @@ function generate(numRows: number): number[][] {
const f: number[][] = [[1]];
for (let i = 0; i < numRows - 1; ++i) {
const g: number[] = [1];
for (let j = 0; j < f[i].length - 1; ++j) {
g.push(f[i][j] + f[i][j + 1]);
for (let j = 1; j < f[i].length; ++j) {
g.push(f[i][j - 1] + f[i][j]);
}
g.push(1);
f.push(g);
Expand All @@ -143,21 +143,17 @@ function generate(numRows: number): number[][] {

```rust
impl Solution {
#[allow(dead_code)]
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
let mut ret_vec: Vec<Vec<i32>> = Vec::new();
let mut cur_vec: Vec<i32> = Vec::new();

for i in 0..num_rows as usize {
let mut new_vec: Vec<i32> = vec![1; i + 1];
for j in 1..i {
new_vec[j] = cur_vec[j - 1] + cur_vec[j];
let mut f = vec![vec![1]];
for i in 1..num_rows {
let mut g = vec![1];
for j in 1..f[i as usize - 1].len() {
g.push(f[i as usize - 1][j - 1] + f[i as usize - 1][j]);
}
cur_vec = new_vec.clone();
ret_vec.push(new_vec);
g.push(1);
f.push(g);
}

ret_vec
f
}
}
```
Expand All @@ -173,8 +169,8 @@ var generate = function (numRows) {
const f = [[1]];
for (let i = 0; i < numRows - 1; ++i) {
const g = [1];
for (let j = 0; j < f[i].length - 1; ++j) {
g.push(f[i][j] + f[i][j + 1]);
for (let j = 1; j < f[i].length; ++j) {
g.push(f[i][j - 1] + f[i][j]);
}
g.push(1);
f.push(g);
Expand All @@ -183,6 +179,25 @@ var generate = function (numRows) {
};
```

#### C#

```cs
public class Solution {
public IList<IList<int>> Generate(int numRows) {
var f = new List<IList<int>> { new List<int> { 1 } };
for (int i = 1; i < numRows; ++i) {
var g = new List<int> { 1 };
for (int j = 1; j < f[i - 1].Count; ++j) {
g.Add(f[i - 1][j - 1] + f[i - 1][j]);
}
g.Add(1);
f.Add(g);
}
return f;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
4 changes: 2 additions & 2 deletions solution/0100-0199/0118.Pascal's Triangle/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Solution {
for (int i = 0; i < numRows - 1; ++i) {
vector<int> g;
g.push_back(1);
for (int j = 0; j < f[i].size() - 1; ++j) {
g.push_back(f[i][j] + f[i][j + 1]);
for (int j = 1; j < f[i].size(); ++j) {
g.push_back(f[i][j - 1] + f[i][j]);
}
g.push_back(1);
f.push_back(g);
Expand Down
14 changes: 14 additions & 0 deletions solution/0100-0199/0118.Pascal's Triangle/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
public IList<IList<int>> Generate(int numRows) {
var f = new List<IList<int>> { new List<int> { 1 } };
for (int i = 1; i < numRows; ++i) {
var g = new List<int> { 1 };
for (int j = 1; j < f[i - 1].Count; ++j) {
g.Add(f[i - 1][j - 1] + f[i - 1][j]);
}
g.Add(1);
f.Add(g);
}
return f;
}
}
4 changes: 2 additions & 2 deletions solution/0100-0199/0118.Pascal's Triangle/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ func generate(numRows int) [][]int {
f := [][]int{[]int{1}}
for i := 0; i < numRows-1; i++ {
g := []int{1}
for j := 0; j < len(f[i])-1; j++ {
g = append(g, f[i][j]+f[i][j+1])
for j := 1; j < len(f[i]); j++ {
g = append(g, f[i][j-1]+f[i][j])
}
g = append(g, 1)
f = append(f, g)
Expand Down
4 changes: 2 additions & 2 deletions solution/0100-0199/0118.Pascal's Triangle/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public List<List<Integer>> generate(int numRows) {
for (int i = 0; i < numRows - 1; ++i) {
List<Integer> g = new ArrayList<>();
g.add(1);
for (int j = 0; j < f.get(i).size() - 1; ++j) {
g.add(f.get(i).get(j) + f.get(i).get(j + 1));
for (int j = 1; j < f.get(i).size(); ++j) {
g.add(f.get(i).get(j - 1) + f.get(i).get(j));
}
g.add(1);
f.add(g);
Expand Down
4 changes: 2 additions & 2 deletions solution/0100-0199/0118.Pascal's Triangle/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var generate = function (numRows) {
const f = [[1]];
for (let i = 0; i < numRows - 1; ++i) {
const g = [1];
for (let j = 0; j < f[i].length - 1; ++j) {
g.push(f[i][j] + f[i][j + 1]);
for (let j = 1; j < f[i].length; ++j) {
g.push(f[i][j - 1] + f[i][j]);
}
g.push(1);
f.push(g);
Expand Down
20 changes: 8 additions & 12 deletions solution/0100-0199/0118.Pascal's Triangle/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
impl Solution {
#[allow(dead_code)]
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
let mut ret_vec: Vec<Vec<i32>> = Vec::new();
let mut cur_vec: Vec<i32> = Vec::new();

for i in 0..num_rows as usize {
let mut new_vec: Vec<i32> = vec![1; i + 1];
for j in 1..i {
new_vec[j] = cur_vec[j - 1] + cur_vec[j];
let mut f = vec![vec![1]];
for i in 1..num_rows {
let mut g = vec![1];
for j in 1..f[i as usize - 1].len() {
g.push(f[i as usize - 1][j - 1] + f[i as usize - 1][j]);
}
cur_vec = new_vec.clone();
ret_vec.push(new_vec);
g.push(1);
f.push(g);
}

ret_vec
f
}
}
4 changes: 2 additions & 2 deletions solution/0100-0199/0118.Pascal's Triangle/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ function generate(numRows: number): number[][] {
const f: number[][] = [[1]];
for (let i = 0; i < numRows - 1; ++i) {
const g: number[] = [1];
for (let j = 0; j < f[i].length - 1; ++j) {
g.push(f[i][j] + f[i][j + 1]);
for (let j = 1; j < f[i].length; ++j) {
g.push(f[i][j - 1] + f[i][j]);
}
g.push(1);
f.push(g);
Expand Down