Skip to content
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

feat: update lc problems #3778

Merged
merged 1 commit into from
Nov 19, 2024
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
8 changes: 4 additions & 4 deletions lcp/LCP 22. 黑白方格画/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Solution {
if k == 0 || k == n * n {
return 1
}

func combination(_ n: Int, _ r: Int) -> Int {
guard r <= n else { return 0 }
if r == 0 || r == n { return 1 }
Expand All @@ -213,9 +213,9 @@ class Solution {
}
return result
}

var ans = 0

for i in 0...n {
for j in 0...n {
let paintedCells = n * (i + j) - i * j
Expand All @@ -224,7 +224,7 @@ class Solution {
}
}
}

return ans
}
}
Expand Down
6 changes: 3 additions & 3 deletions lcp/LCP 24. 数字游戏/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,12 @@ class MedianFinder {
init() {}

func addNum(_ num: Int) {

upperHeap.append(num)
sumUpper += num
upperHeap.sort()


if let minUpper = upperHeap.first {
upperHeap.removeFirst()
lowerHeap.append(minUpper)
Expand All @@ -334,7 +334,7 @@ class MedianFinder {
lowerHeap.sort(by: >)
}


if lowerHeap.count > upperHeap.count + 1 {
if let maxLower = lowerHeap.first {
lowerHeap.removeFirst()
Expand Down
8 changes: 4 additions & 4 deletions lcp/LCP 25. 古董键盘/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,26 +203,26 @@ class Solution {
for i in 0...n {
c[i][0] = 1
}

for i in 1...n {
for j in 1...k {
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod
}
}

var f = Array(repeating: Array(repeating: 0, count: 27), count: n + 1)
for j in 0..<27 {
f[0][j] = 1
}

for i in 1...n {
for j in 1..<27 {
for h in 0...min(i, k) {
f[i][j] = (f[i][j] + (f[i - h][j - 1] * c[i][h]) % mod) % mod
}
}
}

return f[n][26]
}
}
Expand Down
36 changes: 30 additions & 6 deletions solution/0800-0899/0867.Transpose Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们记矩阵 $\textit{matrix}$ 的行数为 $m$,列数为 $n$。根据转置的定义,转置后的矩阵 $\textit{ans}$ 的行数为 $n$,列数为 $m$。

对于 $\textit{ans}$ 中的任意位置 $(i,j)$,其对应于矩阵 $\textit{matrix}$ 中的位置 $(j,i)$。因此,我们遍历矩阵 $\textit{matrix}$ 中的每个元素,将其转置到 $\textit{ans}$ 中相应的位置。

遍历结束后,返回 $\textit{ans}$ 即可。

时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵 $\textit{matrix}$ 的行数和列数。忽略答案的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -95,9 +103,11 @@ public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
};
Expand All @@ -119,6 +129,21 @@ func transpose(matrix [][]int) [][]int {
}
```

#### TypeScript

```ts
function transpose(matrix: number[][]): number[][] {
const [m, n] = [matrix.length, matrix[0].length];
const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
```

#### JavaScript

```js
Expand All @@ -127,9 +152,8 @@ func transpose(matrix [][]int) [][]int {
* @return {number[][]}
*/
var transpose = function (matrix) {
const m = matrix.length;
const n = matrix[0].length;
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
const [m, n] = [matrix.length, matrix[0].length];
const ans = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
Expand Down
36 changes: 30 additions & 6 deletions solution/0800-0899/0867.Transpose Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

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}$.

After the traversal, we return $\textit{ans}$.

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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -93,9 +101,11 @@ public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
};
Expand All @@ -117,6 +127,21 @@ func transpose(matrix [][]int) [][]int {
}
```

#### TypeScript

```ts
function transpose(matrix: number[][]): number[][] {
const [m, n] = [matrix.length, matrix[0].length];
const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
```

#### JavaScript

```js
Expand All @@ -125,9 +150,8 @@ func transpose(matrix [][]int) [][]int {
* @return {number[][]}
*/
var transpose = function (matrix) {
const m = matrix.length;
const n = matrix[0].length;
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
const [m, n] = [matrix.length, matrix[0].length];
const ans = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
Expand Down
8 changes: 5 additions & 3 deletions solution/0800-0899/0867.Transpose Matrix/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ class Solution {
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
};
};
5 changes: 2 additions & 3 deletions solution/0800-0899/0867.Transpose Matrix/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
* @return {number[][]}
*/
var transpose = function (matrix) {
const m = matrix.length;
const n = matrix[0].length;
const ans = new Array(n).fill(0).map(() => new Array(m).fill(0));
const [m, n] = [matrix.length, matrix[0].length];
const ans = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
Expand Down
10 changes: 10 additions & 0 deletions solution/0800-0899/0867.Transpose Matrix/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function transpose(matrix: number[][]): number[][] {
const [m, n] = [matrix.length, matrix[0].length];
const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
84 changes: 41 additions & 43 deletions solution/0800-0899/0868.Binary Gap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:位运算

我们用两个指针 $\textit{pre}$ 和 $\textit{cur}$ 分别表示上一个和当前的 $1$ 的位置,初始时 $\textit{pre} = 100$, $\textit{cur} = 0$,然后遍历 $n$ 的二进制表示,当遇到 $1$ 时,计算当前位置和上一个 $1$ 的位置之间的距离,并更新答案。

时间复杂度 $O(\log n)$,其中 $n$ 是题目给定的整数。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -77,12 +81,13 @@ tags:
```python
class Solution:
def binaryGap(self, n: int) -> int:
ans, j = 0, -1
for i in range(32):
ans = 0
pre, cur = inf, 0
while n:
if n & 1:
if j != -1:
ans = max(ans, i - j)
j = i
ans = max(ans, cur - pre)
pre = cur
cur += 1
n >>= 1
return ans
```
Expand All @@ -93,13 +98,12 @@ class Solution:
class Solution {
public int binaryGap(int n) {
int ans = 0;
for (int i = 0, j = -1; n != 0; ++i, n >>= 1) {
if ((n & 1) == 1) {
if (j != -1) {
ans = Math.max(ans, i - j);
}
j = i;
for (int pre = 100, cur = 0; n != 0; n >>= 1) {
if (n % 2 == 1) {
ans = Math.max(ans, cur - pre);
pre = cur;
}
++cur;
}
return ans;
}
Expand All @@ -113,11 +117,12 @@ class Solution {
public:
int binaryGap(int n) {
int ans = 0;
for (int i = 0, j = -1; n; ++i, n >>= 1) {
for (int pre = 100, cur = 0; n != 0; n >>= 1) {
if (n & 1) {
if (j != -1) ans = max(ans, i - j);
j = i;
ans = max(ans, cur - pre);
pre = cur;
}
++cur;
}
return ans;
}
Expand All @@ -127,36 +132,31 @@ public:
#### Go

```go
func binaryGap(n int) int {
ans := 0
for i, j := 0, -1; n != 0; i, n = i+1, n>>1 {
if (n & 1) == 1 {
if j != -1 && ans < i-j {
ans = i - j
}
j = i
func binaryGap(n int) (ans int) {
for pre, cur := 100, 0; n != 0; n >>= 1 {
if n&1 == 1 {
ans = max(ans, cur-pre)
pre = cur
}
cur++
}
return ans
return
}
```

#### TypeScript

```ts
function binaryGap(n: number): number {
let res = 0;
let j = -1;
for (let i = 0; n !== 0; i++) {
let ans = 0;
for (let pre = 100, cur = 0; n; n >>= 1) {
if (n & 1) {
if (j !== -1) {
res = Math.max(res, i - j);
}
j = i;
ans = Math.max(ans, cur - pre);
pre = cur;
}
n >>= 1;
++cur;
}
return res;
return ans;
}
```

Expand All @@ -165,20 +165,18 @@ function binaryGap(n: number): number {
```rust
impl Solution {
pub fn binary_gap(mut n: i32) -> i32 {
let mut res = 0;
let mut i = 0;
let mut j = -1;
let mut ans = 0;
let mut pre = 100;
let mut cur = 0;
while n != 0 {
if (n & 1) == 1 {
if j != -1 {
res = res.max(i - j);
}
j = i;
if n % 2 == 1 {
ans = ans.max(cur - pre);
pre = cur;
}
cur += 1;
n >>= 1;
i += 1;
}
res
ans
}
}
```
Expand Down
Loading
Loading