Skip to content

Commit db9832c

Browse files
committed
feat: add solutions to lc problem: No.0566
No.0566.Reshape the Matrix
1 parent abb1631 commit db9832c

File tree

7 files changed

+160
-40
lines changed

7 files changed

+160
-40
lines changed

solution/0500-0599/0555.Split Concatenated Strings/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
我们先遍历字符串数组 `strs`,对于每个字符串 $s$,如果 $s$ 的反转字符串 $t$ 比 $s$ 大,那么我们就将 $s$ 替换为 $t$。
5656

57-
然后我们再枚举字符串数组 `strs` 的每个位置 $i$ 作为分割点,将字符串数组 `strs` 拆成两部分,分别为 $strs[i + 1:]$ 和 $strs[:i]$,然后将这两部分拼接起来,得到一个新的字符串 $t$。接下来,我们枚举当前字符串 $strs[i]$ 的每个位置 $j$,其后缀部分为 $a=strs[i][j:]$,前缀部分为 $b=strs[i][:j]$,那么我们可以将 $a$$t$ 和 $b$ 拼接起来,得到一个新的字符串 $cur$,如果 $cur$ 比当前答案大,那么我们就更新答案。这是将 $strs[i]$ 翻转后的情况,我们还需要考虑 $strs[i]$ 不翻转的情况,即将 $a$$t$ 和 $b$ 的顺序反过来拼接,得到一个新的字符串 $cur$,如果 $cur$ 比当前答案大,那么我们就更新答案。
57+
然后我们再枚举字符串数组 `strs` 的每个位置 $i$ 作为分割点,将字符串数组 `strs` 拆成两部分,分别为 $strs[i + 1:]$ 和 $strs[:i]$,然后将这两部分拼接起来,得到一个新的字符串 $t$。接下来,我们枚举当前字符串 $strs[i]$ 的每个位置 $j$,其后缀部分为 $a=strs[i][j:]$,前缀部分为 $b=strs[i][:j]$,那么我们可以将 $a$, $t$ 和 $b$ 拼接起来,得到一个新的字符串 $cur$,如果 $cur$ 比当前答案大,那么我们就更新答案。这是将 $strs[i]$ 翻转后的情况,我们还需要考虑 $strs[i]$ 不翻转的情况,即将 $a$, $t$ 和 $b$ 的顺序反过来拼接,得到一个新的字符串 $cur$,如果 $cur$ 比当前答案大,那么我们就更新答案。
5858

5959
最后,我们返回答案即可。
6060

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

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:模拟**
50+
51+
我们先获取原矩阵的行数和列数,分别记为 $m$ 和 $n$。如果 $m \times n \neq r \times c$,则无法重塑矩阵,直接返回原矩阵。
52+
53+
否则,我们创建一个新矩阵,新矩阵的行数为 $r$,列数为 $c$。我们从原矩阵的第一个元素开始,按照行优先的顺序遍历原矩阵的所有元素,将遍历到的元素按顺序放入新矩阵中。
54+
55+
遍历完原矩阵的所有元素后,我们即可得到答案。
56+
57+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是原矩阵的行数和列数。忽略答案的空间消耗,空间复杂度 $O(1)$。
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
@@ -54,14 +64,14 @@
5464

5565
```python
5666
class Solution:
57-
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
58-
m, n = len(nums), len(nums[0])
67+
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
68+
m, n = len(mat), len(mat[0])
5969
if m * n != r * c:
60-
return nums
61-
res = [[0] * c for _ in range(r)]
62-
for x in range(m * n):
63-
res[x // c][x % c] = nums[x // n][x % n]
64-
return res
70+
return mat
71+
ans = [[0] * c for _ in range(r)]
72+
for i in range(m * n):
73+
ans[i // c][i % c] = mat[i // n][i % n]
74+
return ans
6575
```
6676

6777
### **Java**
@@ -70,18 +80,58 @@ class Solution:
7080

7181
```java
7282
class Solution {
73-
public int[][] matrixReshape(int[][] nums, int r, int c) {
74-
int m = nums.length, n = nums[0].length;
75-
if (m * n != r * c) return nums;
76-
int[][] res = new int[r][c];
83+
public int[][] matrixReshape(int[][] mat, int r, int c) {
84+
int m = mat.length, n = mat[0].length;
85+
if (m * n != r * c) {
86+
return mat;
87+
}
88+
int[][] ans = new int[r][c];
7789
for (int i = 0; i < m * n; ++i) {
78-
res[i / c][i % c] = nums[i / n][i % n];
90+
ans[i / c][i % c] = mat[i / n][i % n];
7991
}
80-
return res;
92+
return ans;
8193
}
8294
}
8395
```
8496

97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
103+
int m = mat.size(), n = mat[0].size();
104+
if (m * n != r * c) {
105+
return mat;
106+
}
107+
vector<vector<int>> ans(r, vector<int>(c));
108+
for (int i = 0; i < m * n; ++i) {
109+
ans[i / c][i % c] = mat[i / n][i % n];
110+
}
111+
return ans;
112+
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func matrixReshape(mat [][]int, r int, c int) [][]int {
120+
m, n := len(mat), len(mat[0])
121+
if m*n != r*c {
122+
return mat
123+
}
124+
ans := make([][]int, r)
125+
for i := range ans {
126+
ans[i] = make([]int, c)
127+
}
128+
for i := 0; i < m*n; i++ {
129+
ans[i/c][i%c] = mat[i/n][i%n]
130+
}
131+
return ans
132+
}
133+
```
134+
85135
### **TypeScript**
86136

87137
```ts

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

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,69 @@
4646

4747
```python
4848
class Solution:
49-
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
50-
m, n = len(nums), len(nums[0])
49+
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
50+
m, n = len(mat), len(mat[0])
5151
if m * n != r * c:
52-
return nums
53-
res = [[0] * c for _ in range(r)]
54-
for x in range(m * n):
55-
res[x // c][x % c] = nums[x // n][x % n]
56-
return res
52+
return mat
53+
ans = [[0] * c for _ in range(r)]
54+
for i in range(m * n):
55+
ans[i // c][i % c] = mat[i // n][i % n]
56+
return ans
5757
```
5858

5959
### **Java**
6060

6161
```java
6262
class Solution {
63-
public int[][] matrixReshape(int[][] nums, int r, int c) {
64-
int m = nums.length, n = nums[0].length;
65-
if (m * n != r * c) return nums;
66-
int[][] res = new int[r][c];
63+
public int[][] matrixReshape(int[][] mat, int r, int c) {
64+
int m = mat.length, n = mat[0].length;
65+
if (m * n != r * c) {
66+
return mat;
67+
}
68+
int[][] ans = new int[r][c];
69+
for (int i = 0; i < m * n; ++i) {
70+
ans[i / c][i % c] = mat[i / n][i % n];
71+
}
72+
return ans;
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
83+
int m = mat.size(), n = mat[0].size();
84+
if (m * n != r * c) {
85+
return mat;
86+
}
87+
vector<vector<int>> ans(r, vector<int>(c));
6788
for (int i = 0; i < m * n; ++i) {
68-
res[i / c][i % c] = nums[i / n][i % n];
89+
ans[i / c][i % c] = mat[i / n][i % n];
6990
}
70-
return res;
91+
return ans;
7192
}
93+
};
94+
```
95+
96+
### **Go**
97+
98+
```go
99+
func matrixReshape(mat [][]int, r int, c int) [][]int {
100+
m, n := len(mat), len(mat[0])
101+
if m*n != r*c {
102+
return mat
103+
}
104+
ans := make([][]int, r)
105+
for i := range ans {
106+
ans[i] = make([]int, c)
107+
}
108+
for i := 0; i < m*n; i++ {
109+
ans[i/c][i%c] = mat[i/n][i%n]
110+
}
111+
return ans
72112
}
73113
```
74114

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
4+
int m = mat.size(), n = mat[0].size();
5+
if (m * n != r * c) {
6+
return mat;
7+
}
8+
vector<vector<int>> ans(r, vector<int>(c));
9+
for (int i = 0; i < m * n; ++i) {
10+
ans[i / c][i % c] = mat[i / n][i % n];
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func matrixReshape(mat [][]int, r int, c int) [][]int {
2+
m, n := len(mat), len(mat[0])
3+
if m*n != r*c {
4+
return mat
5+
}
6+
ans := make([][]int, r)
7+
for i := range ans {
8+
ans[i] = make([]int, c)
9+
}
10+
for i := 0; i < m*n; i++ {
11+
ans[i/c][i%c] = mat[i/n][i%n]
12+
}
13+
return ans
14+
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class Solution {
2-
public int[][] matrixReshape(int[][] nums, int r, int c) {
3-
int m = nums.length, n = nums[0].length;
4-
if (m * n != r * c) return nums;
5-
int[][] res = new int[r][c];
2+
public int[][] matrixReshape(int[][] mat, int r, int c) {
3+
int m = mat.length, n = mat[0].length;
4+
if (m * n != r * c) {
5+
return mat;
6+
}
7+
int[][] ans = new int[r][c];
68
for (int i = 0; i < m * n; ++i) {
7-
res[i / c][i % c] = nums[i / n][i % n];
9+
ans[i / c][i % c] = mat[i / n][i % n];
810
}
9-
return res;
11+
return ans;
1012
}
1113
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
2-
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
3-
m, n = len(nums), len(nums[0])
2+
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
3+
m, n = len(mat), len(mat[0])
44
if m * n != r * c:
5-
return nums
6-
res = [[0] * c for _ in range(r)]
7-
for x in range(m * n):
8-
res[x // c][x % c] = nums[x // n][x % n]
9-
return res
5+
return mat
6+
ans = [[0] * c for _ in range(r)]
7+
for i in range(m * n):
8+
ans[i // c][i % c] = mat[i // n][i % n]
9+
return ans

0 commit comments

Comments
 (0)