Skip to content

Commit bbb63be

Browse files
authored
feat: update lc problems (#3778)
1 parent da7abd7 commit bbb63be

File tree

31 files changed

+297
-211
lines changed

31 files changed

+297
-211
lines changed

lcp/LCP 22. 黑白方格画/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class Solution {
203203
if k == 0 || k == n * n {
204204
return 1
205205
}
206-
206+
207207
func combination(_ n: Int, _ r: Int) -> Int {
208208
guard r <= n else { return 0 }
209209
if r == 0 || r == n { return 1 }
@@ -213,9 +213,9 @@ class Solution {
213213
}
214214
return result
215215
}
216-
216+
217217
var ans = 0
218-
218+
219219
for i in 0...n {
220220
for j in 0...n {
221221
let paintedCells = n * (i + j) - i * j
@@ -224,7 +224,7 @@ class Solution {
224224
}
225225
}
226226
}
227-
227+
228228
return ans
229229
}
230230
}

lcp/LCP 24. 数字游戏/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,12 @@ class MedianFinder {
320320
init() {}
321321

322322
func addNum(_ num: Int) {
323-
323+
324324
upperHeap.append(num)
325325
sumUpper += num
326326
upperHeap.sort()
327327

328-
328+
329329
if let minUpper = upperHeap.first {
330330
upperHeap.removeFirst()
331331
lowerHeap.append(minUpper)
@@ -334,7 +334,7 @@ class MedianFinder {
334334
lowerHeap.sort(by: >)
335335
}
336336

337-
337+
338338
if lowerHeap.count > upperHeap.count + 1 {
339339
if let maxLower = lowerHeap.first {
340340
lowerHeap.removeFirst()

lcp/LCP 25. 古董键盘/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,26 @@ class Solution {
203203
for i in 0...n {
204204
c[i][0] = 1
205205
}
206-
206+
207207
for i in 1...n {
208208
for j in 1...k {
209209
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod
210210
}
211211
}
212-
212+
213213
var f = Array(repeating: Array(repeating: 0, count: 27), count: n + 1)
214214
for j in 0..<27 {
215215
f[0][j] = 1
216216
}
217-
217+
218218
for i in 1...n {
219219
for j in 1..<27 {
220220
for h in 0...min(i, k) {
221221
f[i][j] = (f[i][j] + (f[i - h][j - 1] * c[i][h]) % mod) % mod
222222
}
223223
}
224224
}
225-
225+
226226
return f[n][26]
227227
}
228228
}

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

+30-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### 方法一
61+
### 方法一:模拟
62+
63+
我们记矩阵 $\textit{matrix}$ 的行数为 $m$,列数为 $n$。根据转置的定义,转置后的矩阵 $\textit{ans}$ 的行数为 $n$,列数为 $m$。
64+
65+
对于 $\textit{ans}$ 中的任意位置 $(i,j)$,其对应于矩阵 $\textit{matrix}$ 中的位置 $(j,i)$。因此,我们遍历矩阵 $\textit{matrix}$ 中的每个元素,将其转置到 $\textit{ans}$ 中相应的位置。
66+
67+
遍历结束后,返回 $\textit{ans}$ 即可。
68+
69+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵 $\textit{matrix}$ 的行数和列数。忽略答案的空间消耗,空间复杂度 $O(1)$。
6270

6371
<!-- tabs:start -->
6472

@@ -95,9 +103,11 @@ public:
95103
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
96104
int m = matrix.size(), n = matrix[0].size();
97105
vector<vector<int>> ans(n, vector<int>(m));
98-
for (int i = 0; i < n; ++i)
99-
for (int j = 0; j < m; ++j)
106+
for (int i = 0; i < n; ++i) {
107+
for (int j = 0; j < m; ++j) {
100108
ans[i][j] = matrix[j][i];
109+
}
110+
}
101111
return ans;
102112
}
103113
};
@@ -119,6 +129,21 @@ func transpose(matrix [][]int) [][]int {
119129
}
120130
```
121131

132+
#### TypeScript
133+
134+
```ts
135+
function transpose(matrix: number[][]): number[][] {
136+
const [m, n] = [matrix.length, matrix[0].length];
137+
const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0));
138+
for (let i = 0; i < n; ++i) {
139+
for (let j = 0; j < m; ++j) {
140+
ans[i][j] = matrix[j][i];
141+
}
142+
}
143+
return ans;
144+
}
145+
```
146+
122147
#### JavaScript
123148

124149
```js
@@ -127,9 +152,8 @@ func transpose(matrix [][]int) [][]int {
127152
* @return {number[][]}
128153
*/
129154
var transpose = function (matrix) {
130-
const m = matrix.length;
131-
const n = matrix[0].length;
132-
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
155+
const [m, n] = [matrix.length, matrix[0].length];
156+
const ans = Array.from({ length: n }, () => Array(m).fill(0));
133157
for (let i = 0; i < n; ++i) {
134158
for (let j = 0; j < m; ++j) {
135159
ans[i][j] = matrix[j][i];

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

+30-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Simulation
60+
61+
Let $m$ be the number of rows and $n$ be the number of columns in the matrix $\textit{matrix}$. According to the definition of transpose, the transposed matrix $\textit{ans}$ will have $n$ rows and $m$ columns.
62+
63+
For any position $(i, j)$ in $\textit{ans}$, it corresponds to the position $(j, i)$ in the matrix $\textit{matrix}$. Therefore, we traverse each element in the matrix $\textit{matrix}$ and transpose it to the corresponding position in $\textit{ans}$.
64+
65+
After the traversal, we return $\textit{ans}$.
66+
67+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the matrix $\textit{matrix}$, respectively. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
6068

6169
<!-- tabs:start -->
6270

@@ -93,9 +101,11 @@ public:
93101
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
94102
int m = matrix.size(), n = matrix[0].size();
95103
vector<vector<int>> ans(n, vector<int>(m));
96-
for (int i = 0; i < n; ++i)
97-
for (int j = 0; j < m; ++j)
104+
for (int i = 0; i < n; ++i) {
105+
for (int j = 0; j < m; ++j) {
98106
ans[i][j] = matrix[j][i];
107+
}
108+
}
99109
return ans;
100110
}
101111
};
@@ -117,6 +127,21 @@ func transpose(matrix [][]int) [][]int {
117127
}
118128
```
119129

130+
#### TypeScript
131+
132+
```ts
133+
function transpose(matrix: number[][]): number[][] {
134+
const [m, n] = [matrix.length, matrix[0].length];
135+
const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0));
136+
for (let i = 0; i < n; ++i) {
137+
for (let j = 0; j < m; ++j) {
138+
ans[i][j] = matrix[j][i];
139+
}
140+
}
141+
return ans;
142+
}
143+
```
144+
120145
#### JavaScript
121146

122147
```js
@@ -125,9 +150,8 @@ func transpose(matrix [][]int) [][]int {
125150
* @return {number[][]}
126151
*/
127152
var transpose = function (matrix) {
128-
const m = matrix.length;
129-
const n = matrix[0].length;
130-
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
153+
const [m, n] = [matrix.length, matrix[0].length];
154+
const ans = Array.from({ length: n }, () => Array(m).fill(0));
131155
for (let i = 0; i < n; ++i) {
132156
for (let j = 0; j < m; ++j) {
133157
ans[i][j] = matrix[j][i];

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ class Solution {
33
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
44
int m = matrix.size(), n = matrix[0].size();
55
vector<vector<int>> ans(n, vector<int>(m));
6-
for (int i = 0; i < n; ++i)
7-
for (int j = 0; j < m; ++j)
6+
for (int i = 0; i < n; ++i) {
7+
for (int j = 0; j < m; ++j) {
88
ans[i][j] = matrix[j][i];
9+
}
10+
}
911
return ans;
1012
}
11-
};
13+
};

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* @return {number[][]}
44
*/
55
var transpose = function (matrix) {
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));
6+
const [m, n] = [matrix.length, matrix[0].length];
7+
const ans = Array.from({ length: n }, () => Array(m).fill(0));
98
for (let i = 0; i < n; ++i) {
109
for (let j = 0; j < m; ++j) {
1110
ans[i][j] = matrix[j][i];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function transpose(matrix: number[][]): number[][] {
2+
const [m, n] = [matrix.length, matrix[0].length];
3+
const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0));
4+
for (let i = 0; i < n; ++i) {
5+
for (let j = 0; j < m; ++j) {
6+
ans[i][j] = matrix[j][i];
7+
}
8+
}
9+
return ans;
10+
}

solution/0800-0899/0868.Binary Gap/README.md

+41-43
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### 方法一
71+
### 方法一:位运算
72+
73+
我们用两个指针 $\textit{pre}$ 和 $\textit{cur}$ 分别表示上一个和当前的 $1$ 的位置,初始时 $\textit{pre} = 100$, $\textit{cur} = 0$,然后遍历 $n$ 的二进制表示,当遇到 $1$ 时,计算当前位置和上一个 $1$ 的位置之间的距离,并更新答案。
74+
75+
时间复杂度 $O(\log n)$,其中 $n$ 是题目给定的整数。空间复杂度 $O(1)$。
7276

7377
<!-- tabs:start -->
7478

@@ -77,12 +81,13 @@ tags:
7781
```python
7882
class Solution:
7983
def binaryGap(self, n: int) -> int:
80-
ans, j = 0, -1
81-
for i in range(32):
84+
ans = 0
85+
pre, cur = inf, 0
86+
while n:
8287
if n & 1:
83-
if j != -1:
84-
ans = max(ans, i - j)
85-
j = i
88+
ans = max(ans, cur - pre)
89+
pre = cur
90+
cur += 1
8691
n >>= 1
8792
return ans
8893
```
@@ -93,13 +98,12 @@ class Solution:
9398
class Solution {
9499
public int binaryGap(int n) {
95100
int ans = 0;
96-
for (int i = 0, j = -1; n != 0; ++i, n >>= 1) {
97-
if ((n & 1) == 1) {
98-
if (j != -1) {
99-
ans = Math.max(ans, i - j);
100-
}
101-
j = i;
101+
for (int pre = 100, cur = 0; n != 0; n >>= 1) {
102+
if (n % 2 == 1) {
103+
ans = Math.max(ans, cur - pre);
104+
pre = cur;
102105
}
106+
++cur;
103107
}
104108
return ans;
105109
}
@@ -113,11 +117,12 @@ class Solution {
113117
public:
114118
int binaryGap(int n) {
115119
int ans = 0;
116-
for (int i = 0, j = -1; n; ++i, n >>= 1) {
120+
for (int pre = 100, cur = 0; n != 0; n >>= 1) {
117121
if (n & 1) {
118-
if (j != -1) ans = max(ans, i - j);
119-
j = i;
122+
ans = max(ans, cur - pre);
123+
pre = cur;
120124
}
125+
++cur;
121126
}
122127
return ans;
123128
}
@@ -127,36 +132,31 @@ public:
127132
#### Go
128133
129134
```go
130-
func binaryGap(n int) int {
131-
ans := 0
132-
for i, j := 0, -1; n != 0; i, n = i+1, n>>1 {
133-
if (n & 1) == 1 {
134-
if j != -1 && ans < i-j {
135-
ans = i - j
136-
}
137-
j = i
135+
func binaryGap(n int) (ans int) {
136+
for pre, cur := 100, 0; n != 0; n >>= 1 {
137+
if n&1 == 1 {
138+
ans = max(ans, cur-pre)
139+
pre = cur
138140
}
141+
cur++
139142
}
140-
return ans
143+
return
141144
}
142145
```
143146

144147
#### TypeScript
145148

146149
```ts
147150
function binaryGap(n: number): number {
148-
let res = 0;
149-
let j = -1;
150-
for (let i = 0; n !== 0; i++) {
151+
let ans = 0;
152+
for (let pre = 100, cur = 0; n; n >>= 1) {
151153
if (n & 1) {
152-
if (j !== -1) {
153-
res = Math.max(res, i - j);
154-
}
155-
j = i;
154+
ans = Math.max(ans, cur - pre);
155+
pre = cur;
156156
}
157-
n >>= 1;
157+
++cur;
158158
}
159-
return res;
159+
return ans;
160160
}
161161
```
162162

@@ -165,20 +165,18 @@ function binaryGap(n: number): number {
165165
```rust
166166
impl Solution {
167167
pub fn binary_gap(mut n: i32) -> i32 {
168-
let mut res = 0;
169-
let mut i = 0;
170-
let mut j = -1;
168+
let mut ans = 0;
169+
let mut pre = 100;
170+
let mut cur = 0;
171171
while n != 0 {
172-
if (n & 1) == 1 {
173-
if j != -1 {
174-
res = res.max(i - j);
175-
}
176-
j = i;
172+
if n % 2 == 1 {
173+
ans = ans.max(cur - pre);
174+
pre = cur;
177175
}
176+
cur += 1;
178177
n >>= 1;
179-
i += 1;
180178
}
181-
res
179+
ans
182180
}
183181
}
184182
```

0 commit comments

Comments
 (0)