Skip to content

feat: update lc problems #2166

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

Merged
merged 1 commit into from
Dec 29, 2023
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
30 changes: 29 additions & 1 deletion solution/0000-0099/0052.N-Queens II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
```python
class Solution:
def totalNQueens(self, n: int) -> int:
def dfs(i):
def dfs(i: int):
if i == n:
nonlocal ans
ans += 1
Expand Down Expand Up @@ -184,6 +184,34 @@ func totalNQueens(n int) (ans int) {
}
```

### **TypeScript**

```ts
function totalNQueens(n: number): number {
const cols: boolean[] = Array(10).fill(false);
const dg: boolean[] = Array(20).fill(false);
const udg: boolean[] = Array(20).fill(false);
let ans = 0;
const dfs = (i: number) => {
if (i === n) {
++ans;
return;
}
for (let j = 0; j < n; ++j) {
let [a, b] = [i + j, i - j + n];
if (cols[j] || dg[a] || udg[b]) {
continue;
}
cols[j] = dg[a] = udg[b] = true;
dfs(i + 1);
cols[j] = dg[a] = udg[b] = false;
}
};
dfs(0);
return ans;
}
```

### **...**

```
Expand Down
30 changes: 29 additions & 1 deletion solution/0000-0099/0052.N-Queens II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ The time complexity is $O(n!)$, and the space complexity is $O(n)$. Here, $n$ is
```python
class Solution:
def totalNQueens(self, n: int) -> int:
def dfs(i):
def dfs(i: int):
if i == n:
nonlocal ans
ans += 1
Expand Down Expand Up @@ -170,6 +170,34 @@ func totalNQueens(n int) (ans int) {
}
```

### **TypeScript**

```ts
function totalNQueens(n: number): number {
const cols: boolean[] = Array(10).fill(false);
const dg: boolean[] = Array(20).fill(false);
const udg: boolean[] = Array(20).fill(false);
let ans = 0;
const dfs = (i: number) => {
if (i === n) {
++ans;
return;
}
for (let j = 0; j < n; ++j) {
let [a, b] = [i + j, i - j + n];
if (cols[j] || dg[a] || udg[b]) {
continue;
}
cols[j] = dg[a] = udg[b] = true;
dfs(i + 1);
cols[j] = dg[a] = udg[b] = false;
}
};
dfs(0);
return ans;
}
```

### **...**

```
Expand Down
42 changes: 21 additions & 21 deletions solution/0000-0099/0052.N-Queens II/Solution.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
class Solution:
def totalNQueens(self, n: int) -> int:
def dfs(i):
if i == n:
nonlocal ans
ans += 1
return
for j in range(n):
a, b = i + j, i - j + n
if cols[j] or dg[a] or udg[b]:
continue
cols[j] = dg[a] = udg[b] = True
dfs(i + 1)
cols[j] = dg[a] = udg[b] = False

cols = [False] * 10
dg = [False] * 20
udg = [False] * 20
ans = 0
dfs(0)
return ans
class Solution:
def totalNQueens(self, n: int) -> int:
def dfs(i: int):
if i == n:
nonlocal ans
ans += 1
return
for j in range(n):
a, b = i + j, i - j + n
if cols[j] or dg[a] or udg[b]:
continue
cols[j] = dg[a] = udg[b] = True
dfs(i + 1)
cols[j] = dg[a] = udg[b] = False
cols = [False] * 10
dg = [False] * 20
udg = [False] * 20
ans = 0
dfs(0)
return ans
23 changes: 23 additions & 0 deletions solution/0000-0099/0052.N-Queens II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function totalNQueens(n: number): number {
const cols: boolean[] = Array(10).fill(false);
const dg: boolean[] = Array(20).fill(false);
const udg: boolean[] = Array(20).fill(false);
let ans = 0;
const dfs = (i: number) => {
if (i === n) {
++ans;
return;
}
for (let j = 0; j < n; ++j) {
let [a, b] = [i + j, i - j + n];
if (cols[j] || dg[a] || udg[b]) {
continue;
}
cols[j] = dg[a] = udg[b] = true;
dfs(i + 1);
cols[j] = dg[a] = udg[b] = false;
}
};
dfs(0);
return ans;
}
2 changes: 1 addition & 1 deletion solution/0000-0099/0063.Unique Paths II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

首先初始化 $dp$ 第一列和第一行的所有值,然后遍历其它行和列,有两种情况:

- 若 $obstacleGrid[i][j] = 1$,说明路径数为 $0$,那么 $dp[i][j] = 0
- 若 $obstacleGrid[i][j] = 1$,说明路径数为 $0$,那么 $dp[i][j] = 0$
- 若 ¥ obstacleGrid[i][j] = 0$,则 $dp[i][j] = dp[i - 1][j] + dp[i][j - 1]$。

最后返回 $dp[m - 1][n - 1]$ 即可。
Expand Down
2 changes: 1 addition & 1 deletion solution/0000-0099/0065.Valid Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

遍历完字符串后,返回 `true`。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度
时间复杂度 $O(n)$,其中 $n$ 为字符串长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down
117 changes: 55 additions & 62 deletions solution/0000-0099/0069.Sqrt(x)/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@

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

二分查找。
**方法一:二分查找**

我们定义二分查找的左边界 $l = 0$,右边界 $r = x$,然后在 $[l, r]$ 范围内查找平方根。

在每一步查找中,我们找出中间值 $mid = (l + r + 1) / 2$,如果 $mid > x / mid$,说明平方根在 $[l, mid - 1]$ 范围内,我们令 $r = mid - 1$;否则说明平方根在 $[mid, r]$ 范围内,我们令 $l = mid$。

查找结束后,返回 $l$ 即可。

时间复杂度 $O(\log x)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -52,15 +60,14 @@
```python
class Solution:
def mySqrt(self, x: int) -> int:
left, right = 0, x
while left < right:
mid = (left + right + 1) >> 1
# mid*mid <= x
if mid <= x // mid:
left = mid
l, r = 0, x
while l < r:
mid = (l + r + 1) >> 1
if mid > x // mid:
r = mid - 1
else:
right = mid - 1
return left
l = mid
return l
```

### **Java**
Expand All @@ -70,17 +77,16 @@ class Solution:
```java
class Solution {
public int mySqrt(int x) {
int left = 0, right = x;
while (left < right) {
int mid = (left + right + 1) >>> 1;
if (mid <= x / mid) {
// mid*mid <= x
left = mid;
int l = 0, r = x;
while (l < r) {
int mid = (l + r + 1) >>> 1;
if (mid > x / mid) {
r = mid - 1;
} else {
right = mid - 1;
l = mid;
}
}
return left;
return l;
}
}
```
Expand All @@ -91,15 +97,16 @@ class Solution {
class Solution {
public:
int mySqrt(int x) {
long long left = 0, right = x;
while (left < right) {
long long mid = left + ((right - left + 1) >> 1);
if (mid <= x / mid)
left = mid;
else
right = mid - 1;
int l = 0, r = x;
while (l < r) {
int mid = (l + r + 1ll) >> 1;
if (mid > x / mid) {
r = mid - 1;
} else {
l = mid;
}
}
return (int) left;
return l;
}
};
```
Expand All @@ -108,16 +115,7 @@ public:

```go
func mySqrt(x int) int {
left, right := 0, x
for left < right {
mid := left + (right-left+1)>>1
if mid <= x/mid {
left = mid
} else {
right = mid - 1
}
}
return left
return sort.Search(x+1, func(i int) bool { return i*i > x }) - 1
}
```

Expand All @@ -129,17 +127,16 @@ func mySqrt(x int) int {
* @return {number}
*/
var mySqrt = function (x) {
let left = 0;
let right = x;
while (left < right) {
const mid = (left + right + 1) >>> 1;
if (mid <= x / mid) {
left = mid;
let [l, r] = [0, x];
while (l < r) {
const mid = (l + r + 1) >> 1;
if (mid > x / mid) {
r = mid - 1;
} else {
right = mid - 1;
l = mid;
}
}
return left;
return l;
};
```

Expand All @@ -148,20 +145,16 @@ var mySqrt = function (x) {
```cs
public class Solution {
public int MySqrt(int x) {
int left = 0, right = x;
while (left < right)
{
int mid = left + right + 1 >> 1;
if (mid <= x / mid)
{
left = mid;
}
else
{
right = mid - 1;
int l = 0, r = x;
while (l < r) {
int mid = (l + r + 1) >>> 1;
if (mid > x / mid) {
r = mid - 1;
} else {
l = mid;
}
}
return left;
return l;
}
}
```
Expand All @@ -171,19 +164,19 @@ public class Solution {
```rust
impl Solution {
pub fn my_sqrt(x: i32) -> i32 {
if x < 2 {
return x;
}
let mut l = 1;
let mut r = x / 2;
let mut l = 0;
let mut r = x;

while l < r {
let mid = (l + r + 1) >> 1;
if x / mid < mid {
let mid = (l + r + 1) / 2;

if mid > x / mid {
r = mid - 1;
} else {
l = mid;
}
}

l
}
}
Expand Down
Loading