Skip to content

Commit ebd55d1

Browse files
committed
feat: add solutions to lc problems: No.0566,1148,1572
- No.0566.Reshape the Matrix - No.1148.Article Views I - No.1572.Matrix Diagonal Sum
1 parent fb70a72 commit ebd55d1

File tree

9 files changed

+161
-27
lines changed

9 files changed

+161
-27
lines changed

solution/0500-0599/0566.Reshape the Matrix/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,41 @@ function matrixReshape(mat: number[][], r: number, c: number): number[][] {
101101
}
102102
```
103103

104+
### **Rust**
105+
106+
```rust
107+
impl Solution {
108+
pub fn matrix_reshape(mat: Vec<Vec<i32>>, r: i32, c: i32) -> Vec<Vec<i32>> {
109+
let r = r as usize;
110+
let c = c as usize;
111+
let m = mat.len();
112+
let n = mat[0].len();
113+
if m * n != r * c {
114+
return mat;
115+
}
116+
let mut i = 0;
117+
let mut j = 0;
118+
(0..r)
119+
.into_iter()
120+
.map(|_| {
121+
(0..c)
122+
.into_iter()
123+
.map(|_| {
124+
let res = mat[i][j];
125+
j += 1;
126+
if j == n {
127+
j = 0;
128+
i += 1;
129+
}
130+
res
131+
})
132+
.collect()
133+
})
134+
.collect()
135+
}
136+
}
137+
```
138+
104139
### **...**
105140

106141
```

solution/0500-0599/0566.Reshape the Matrix/README_EN.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,41 @@ function matrixReshape(mat: number[][], r: number, c: number): number[][] {
9191
}
9292
```
9393

94+
### **Rust**
95+
96+
```rust
97+
impl Solution {
98+
pub fn matrix_reshape(mat: Vec<Vec<i32>>, r: i32, c: i32) -> Vec<Vec<i32>> {
99+
let r = r as usize;
100+
let c = c as usize;
101+
let m = mat.len();
102+
let n = mat[0].len();
103+
if m * n != r * c {
104+
return mat;
105+
}
106+
let mut i = 0;
107+
let mut j = 0;
108+
(0..r)
109+
.into_iter()
110+
.map(|_| {
111+
(0..c)
112+
.into_iter()
113+
.map(|_| {
114+
let res = mat[i][j];
115+
j += 1;
116+
if j == n {
117+
j = 0;
118+
i += 1;
119+
}
120+
res
121+
})
122+
.collect()
123+
})
124+
.collect()
125+
}
126+
}
127+
```
128+
94129
### **...**
95130

96131
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn matrix_reshape(mat: Vec<Vec<i32>>, r: i32, c: i32) -> Vec<Vec<i32>> {
3+
let r = r as usize;
4+
let c = c as usize;
5+
let m = mat.len();
6+
let n = mat[0].len();
7+
if m * n != r * c {
8+
return mat;
9+
}
10+
let mut i = 0;
11+
let mut j = 0;
12+
(0..r)
13+
.into_iter()
14+
.map(|_| {
15+
(0..c)
16+
.into_iter()
17+
.map(|_| {
18+
let res = mat[i][j];
19+
j += 1;
20+
if j == n {
21+
j = 0;
22+
i += 1;
23+
}
24+
res
25+
})
26+
.collect()
27+
})
28+
.collect()
29+
}
30+
}

solution/1100-1199/1148.Article Views I/README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,10 @@ Views 表:
6262
### **SQL**
6363

6464
```sql
65-
# Write your MySQL query statement below
66-
SELECT
67-
DISTINCT(author_id) as id
68-
FROM
69-
Views
70-
WHERE
71-
author_id = viewer_id
72-
ORDER BY
73-
id;
65+
SELECT DISTINCT(author_id) as id
66+
FROM Views
67+
WHERE author_id = viewer_id
68+
ORDER BY id;
7469
```
7570

7671
<!-- tabs:end -->

solution/1100-1199/1148.Article Views I/README_EN.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,10 @@ Views table:
6161
### **SQL**
6262

6363
```sql
64-
# Write your MySQL query statement below
65-
SELECT
66-
DISTINCT(author_id) as id
67-
FROM
68-
Views
69-
WHERE
70-
author_id = viewer_id
71-
ORDER BY
72-
id;
64+
SELECT DISTINCT(author_id) as id
65+
FROM Views
66+
WHERE author_id = viewer_id
67+
ORDER BY id;
7368
```
7469

7570
<!-- tabs:end -->
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
# Write your MySQL query statement below
2-
SELECT
3-
DISTINCT(author_id) as id
4-
FROM
5-
Views
6-
WHERE
7-
author_id = viewer_id
8-
ORDER BY
9-
id;
1+
SELECT DISTINCT(author_id) as id
2+
FROM Views
3+
WHERE author_id = viewer_id
4+
ORDER BY id;

solution/1500-1599/1572.Matrix Diagonal Sum/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ func diagonalSum(mat [][]int) int {
137137
}
138138
```
139139

140+
### **Rust**
141+
142+
```rust
143+
impl Solution {
144+
pub fn diagonal_sum(mat: Vec<Vec<i32>>) -> i32 {
145+
let n = mat.len();
146+
let mut res = 0;
147+
for i in 0..n {
148+
res += mat[i][i] + mat[n - i - 1][i];
149+
}
150+
if n & 1 == 1 {
151+
return res - mat[n / 2][n / 2];
152+
}
153+
res
154+
}
155+
}
156+
```
157+
140158
### **...**
141159

142160
```

solution/1500-1599/1572.Matrix Diagonal Sum/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ func diagonalSum(mat [][]int) int {
125125
}
126126
```
127127

128+
### **Rust**
129+
130+
```rust
131+
impl Solution {
132+
pub fn diagonal_sum(mat: Vec<Vec<i32>>) -> i32 {
133+
let n = mat.len();
134+
let mut res = 0;
135+
for i in 0..n {
136+
res += mat[i][i] + mat[n - i - 1][i];
137+
}
138+
if n & 1 == 1 {
139+
return res - mat[n / 2][n / 2];
140+
}
141+
res
142+
}
143+
}
144+
```
145+
128146
### **...**
129147

130148
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn diagonal_sum(mat: Vec<Vec<i32>>) -> i32 {
3+
let n = mat.len();
4+
let mut res = 0;
5+
for i in 0..n {
6+
res += mat[i][i] + mat[n - i - 1][i];
7+
}
8+
if n & 1 == 1 {
9+
return res - mat[n / 2][n / 2];
10+
}
11+
res
12+
}
13+
}

0 commit comments

Comments
 (0)