Skip to content

Commit e896f7e

Browse files
committed
feat: add solutions to lc problem: No.0867
No.0867.Transpose Matrix
1 parent 61d0b3c commit e896f7e

File tree

13 files changed

+169
-119
lines changed

13 files changed

+169
-119
lines changed

solution/0800-0899/0867.Transpose Matrix/README.md

+41-15
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@
5353
```python
5454
class Solution:
5555
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
56-
m, n = len(matrix), len(matrix[0])
57-
res = [[0] * m for _ in range(n)]
58-
for i in range(n):
59-
for j in range(m):
60-
res[i][j] = matrix[j][i]
61-
return res
56+
return list(zip(*matrix))
6257
```
6358

6459
### **Java**
@@ -69,17 +64,49 @@ class Solution:
6964
class Solution {
7065
public int[][] transpose(int[][] matrix) {
7166
int m = matrix.length, n = matrix[0].length;
72-
int[][] res = new int[n][m];
67+
int[][] ans = new int[n][m];
7368
for (int i = 0; i < n; ++i) {
7469
for (int j = 0; j < m; ++j) {
75-
res[i][j] = matrix[j][i];
70+
ans[i][j] = matrix[j][i];
7671
}
7772
}
78-
return res;
73+
return ans;
7974
}
8075
}
8176
```
8277

78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
84+
int m = matrix.size(), n = matrix[0].size();
85+
vector<vector<int>> ans(n, vector<int>(m));
86+
for (int i = 0; i < n; ++i)
87+
for (int j = 0; j < m; ++j)
88+
ans[i][j] = matrix[j][i];
89+
return ans;
90+
}
91+
};
92+
```
93+
94+
### **Go**
95+
96+
```go
97+
func transpose(matrix [][]int) [][]int {
98+
m, n := len(matrix), len(matrix[0])
99+
ans := make([][]int, n)
100+
for i := range ans {
101+
ans[i] = make([]int, m)
102+
for j := range ans[i] {
103+
ans[i][j] = matrix[j][i]
104+
}
105+
}
106+
return ans
107+
}
108+
```
109+
83110
### **JavaScript**
84111

85112
```js
@@ -88,16 +115,15 @@ class Solution {
88115
* @return {number[][]}
89116
*/
90117
var transpose = function (matrix) {
91-
const m = matrix.length,
92-
n = matrix[0].length;
93-
let res = [];
118+
const m = matrix.length;
119+
const n = matrix[0].length;
120+
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
94121
for (let i = 0; i < n; ++i) {
95-
res[i] = [];
96122
for (let j = 0; j < m; ++j) {
97-
res[i][j] = matrix[j][i];
123+
ans[i][j] = matrix[j][i];
98124
}
99125
}
100-
return res;
126+
return ans;
101127
};
102128
```
103129

solution/0800-0899/0867.Transpose Matrix/README_EN.md

+41-15
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@
4545
```python
4646
class Solution:
4747
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
48-
m, n = len(matrix), len(matrix[0])
49-
res = [[0] * m for _ in range(n)]
50-
for i in range(n):
51-
for j in range(m):
52-
res[i][j] = matrix[j][i]
53-
return res
48+
return list(zip(*matrix))
5449
```
5550

5651
### **Java**
@@ -59,17 +54,49 @@ class Solution:
5954
class Solution {
6055
public int[][] transpose(int[][] matrix) {
6156
int m = matrix.length, n = matrix[0].length;
62-
int[][] res = new int[n][m];
57+
int[][] ans = new int[n][m];
6358
for (int i = 0; i < n; ++i) {
6459
for (int j = 0; j < m; ++j) {
65-
res[i][j] = matrix[j][i];
60+
ans[i][j] = matrix[j][i];
6661
}
6762
}
68-
return res;
63+
return ans;
6964
}
7065
}
7166
```
7267

68+
### **C++**
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
74+
int m = matrix.size(), n = matrix[0].size();
75+
vector<vector<int>> ans(n, vector<int>(m));
76+
for (int i = 0; i < n; ++i)
77+
for (int j = 0; j < m; ++j)
78+
ans[i][j] = matrix[j][i];
79+
return ans;
80+
}
81+
};
82+
```
83+
84+
### **Go**
85+
86+
```go
87+
func transpose(matrix [][]int) [][]int {
88+
m, n := len(matrix), len(matrix[0])
89+
ans := make([][]int, n)
90+
for i := range ans {
91+
ans[i] = make([]int, m)
92+
for j := range ans[i] {
93+
ans[i][j] = matrix[j][i]
94+
}
95+
}
96+
return ans
97+
}
98+
```
99+
73100
### **JavaScript**
74101

75102
```js
@@ -78,16 +105,15 @@ class Solution {
78105
* @return {number[][]}
79106
*/
80107
var transpose = function (matrix) {
81-
const m = matrix.length,
82-
n = matrix[0].length;
83-
let res = [];
108+
const m = matrix.length;
109+
const n = matrix[0].length;
110+
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
84111
for (let i = 0; i < n; ++i) {
85-
res[i] = [];
86112
for (let j = 0; j < m; ++j) {
87-
res[i][j] = matrix[j][i];
113+
ans[i][j] = matrix[j][i];
88114
}
89115
}
90-
return res;
116+
return ans;
91117
};
92118
```
93119

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
4+
int m = matrix.size(), n = matrix[0].size();
5+
vector<vector<int>> ans(n, vector<int>(m));
6+
for (int i = 0; i < n; ++i)
7+
for (int j = 0; j < m; ++j)
8+
ans[i][j] = matrix[j][i];
9+
return ans;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func transpose(matrix [][]int) [][]int {
2+
m, n := len(matrix), len(matrix[0])
3+
ans := make([][]int, n)
4+
for i := range ans {
5+
ans[i] = make([]int, m)
6+
for j := range ans[i] {
7+
ans[i][j] = matrix[j][i]
8+
}
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public int[][] transpose(int[][] matrix) {
33
int m = matrix.length, n = matrix[0].length;
4-
int[][] res = new int[n][m];
4+
int[][] ans = new int[n][m];
55
for (int i = 0; i < n; ++i) {
66
for (int j = 0; j < m; ++j) {
7-
res[i][j] = matrix[j][i];
7+
ans[i][j] = matrix[j][i];
88
}
99
}
10-
return res;
10+
return ans;
1111
}
1212
}

solution/0800-0899/0867.Transpose Matrix/Solution.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
* @return {number[][]}
44
*/
55
var transpose = function (matrix) {
6-
const m = matrix.length,
7-
n = matrix[0].length;
8-
let res = [];
6+
const m = matrix.length;
7+
const n = matrix[0].length;
8+
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
99
for (let i = 0; i < n; ++i) {
10-
res[i] = [];
1110
for (let j = 0; j < m; ++j) {
12-
res[i][j] = matrix[j][i];
11+
ans[i][j] = matrix[j][i];
1312
}
1413
}
15-
return res;
14+
return ans;
1615
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
class Solution:
22
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
3-
m, n = len(matrix), len(matrix[0])
4-
res = [[0] * m for _ in range(n)]
5-
for i in range(n):
6-
for j in range(m):
7-
res[i][j] = matrix[j][i]
8-
return res
3+
return list(zip(*matrix))

solution/0800-0899/0869.Reordered Power of 2/README.md

+19-25
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
class Solution:
5252
def reorderedPowerOf2(self, n: int) -> bool:
5353
def convert(n):
54-
counter = [0] * 10
55-
while n > 0:
56-
counter[n % 10] += 1
57-
n //= 10
58-
return counter
54+
cnt = [0] * 10
55+
while n:
56+
n, v = divmod(n, 10)
57+
cnt[v] += 1
58+
return cnt
5959

6060
i, s = 1, convert(n)
61-
while i <= 10 ** 9:
61+
while i <= 10**9:
6262
if convert(i) == s:
6363
return True
6464
i <<= 1
@@ -82,12 +82,11 @@ class Solution {
8282
}
8383

8484
private String convert(int n) {
85-
char[] counter = new char[10];
86-
while (n > 0) {
87-
++counter[n % 10];
88-
n /= 10;
85+
char[] cnt = new char[10];
86+
for (; n > 0; n /= 10) {
87+
cnt[n % 10]++;
8988
}
90-
return new String(counter);
89+
return new String(cnt);
9190
}
9291
}
9392
```
@@ -100,18 +99,15 @@ public:
10099
bool reorderedPowerOf2(int n) {
101100
vector<int> s = convert(n);
102101
for (int i = 1; i <= pow(10, 9); i <<= 1)
103-
if (s == convert(i)) return true;
102+
if (s == convert(i))
103+
return true;
104104
return false;
105105
}
106106

107107
vector<int> convert(int n) {
108-
vector<int> counter(10);
109-
while (n)
110-
{
111-
++counter[n % 10];
112-
n /= 10;
113-
}
114-
return counter;
108+
vector<int> cnt(10);
109+
for (; n; n /= 10) ++cnt[n % 10];
110+
return cnt;
115111
}
116112
};
117113
```
@@ -121,14 +117,12 @@ public:
121117
```go
122118
func reorderedPowerOf2(n int) bool {
123119
convert := func(n int) []byte {
124-
counter := make([]byte, 10)
125-
for n > 0 {
126-
counter[n%10]++
127-
n /= 10
120+
cnt := make([]byte, 10)
121+
for ; n > 0; n /= 10 {
122+
cnt[n%10]++
128123
}
129-
return counter
124+
return cnt
130125
}
131-
132126
s := convert(n)
133127
for i := 1; i <= 1e9; i <<= 1 {
134128
if bytes.Equal(s, convert(i)) {

0 commit comments

Comments
 (0)