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: add solutions to lc problems #2601

Merged
merged 1 commit into from
Apr 16, 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
feat: add solutions to lc problems
  • Loading branch information
yanglbme committed Apr 16, 2024
commit a3b1d66ee1bdfaf00a63b71a317e3ad1fc81caa7
6 changes: 3 additions & 3 deletions solution/0900-0999/0908.Smallest Range I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func smallestRangeI(nums []int, k int) int {

```ts
function smallestRangeI(nums: number[], k: number): number {
const max = nums.reduce((r, v) => Math.max(r, v));
const min = nums.reduce((r, v) => Math.min(r, v));
return Math.max(max - min - k * 2, 0);
const mx = Math.max(...nums);
const mi = Math.min(...nums);
return Math.max(mx - mi - k * 2, 0);
}
```

Expand Down
6 changes: 3 additions & 3 deletions solution/0900-0999/0908.Smallest Range I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func smallestRangeI(nums []int, k int) int {

```ts
function smallestRangeI(nums: number[], k: number): number {
const max = nums.reduce((r, v) => Math.max(r, v));
const min = nums.reduce((r, v) => Math.min(r, v));
return Math.max(max - min - k * 2, 0);
const mx = Math.max(...nums);
const mi = Math.min(...nums);
return Math.max(mx - mi - k * 2, 0);
}
```

Expand Down
6 changes: 3 additions & 3 deletions solution/0900-0999/0908.Smallest Range I/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function smallestRangeI(nums: number[], k: number): number {
const max = nums.reduce((r, v) => Math.max(r, v));
const min = nums.reduce((r, v) => Math.min(r, v));
return Math.max(max - min - k * 2, 0);
const mx = Math.max(...nums);
const mi = Math.min(...nums);
return Math.max(mx - mi - k * 2, 0);
}
13 changes: 13 additions & 0 deletions solution/0900-0999/0910.Smallest Range II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ func smallestRangeII(nums []int, k int) int {
}
```

```ts
function smallestRangeII(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
let ans = nums.at(-1)! - nums[0];
for (let i = 1; i < nums.length; ++i) {
const mi = Math.min(nums[0] + k, nums[i] - k);
const mx = Math.max(nums.at(-1)! - k, nums[i - 1] + k);
ans = Math.min(ans, mx - mi);
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
13 changes: 13 additions & 0 deletions solution/0900-0999/0910.Smallest Range II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ func smallestRangeII(nums []int, k int) int {
}
```

```ts
function smallestRangeII(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
let ans = nums.at(-1)! - nums[0];
for (let i = 1; i < nums.length; ++i) {
const mi = Math.min(nums[0] + k, nums[i] - k);
const mx = Math.max(nums.at(-1)! - k, nums[i - 1] + k);
ans = Math.min(ans, mx - mi);
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
10 changes: 10 additions & 0 deletions solution/0900-0999/0910.Smallest Range II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function smallestRangeII(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
let ans = nums.at(-1)! - nums[0];
for (let i = 1; i < nums.length; ++i) {
const mi = Math.min(nums[0] + k, nums[i] - k);
const mx = Math.max(nums.at(-1)! - k, nums[i - 1] + k);
ans = Math.min(ans, mx - mi);
}
return ans;
}
72 changes: 40 additions & 32 deletions solution/0900-0999/0917.Reverse Only Letters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,51 @@

## 解法

### 方法一
### 方法一:双指针

我们用两个指针 $i$ 和 $j$ 分别指向字符串的头部和尾部。当 $i < j$ 时,我们不断地移动 $i$ 和 $j$,直到 $i$ 指向一个英文字母,并且 $j$ 指向一个英文字母,然后交换 $s[i]$ 和 $s[j]$。最后返回字符串即可。

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

<!-- tabs:start -->

```python
class Solution:
def reverseOnlyLetters(self, s: str) -> str:
s = list(s)
i, j = 0, len(s) - 1
cs = list(s)
i, j = 0, len(cs) - 1
while i < j:
while i < j and not s[i].isalpha():
while i < j and not cs[i].isalpha():
i += 1
while i < j and not s[j].isalpha():
while i < j and not cs[j].isalpha():
j -= 1
if i < j:
s[i], s[j] = s[j], s[i]
cs[i], cs[j] = cs[j], cs[i]
i, j = i + 1, j - 1
return ''.join(s)
return "".join(cs)
```

```java
class Solution {
public String reverseOnlyLetters(String s) {
char[] chars = s.toCharArray();
int i = 0, j = s.length() - 1;
char[] cs = s.toCharArray();
int i = 0, j = cs.length - 1;
while (i < j) {
while (i < j && !Character.isLetter(chars[i])) {
while (i < j && !Character.isLetter(cs[i])) {
++i;
}
while (i < j && !Character.isLetter(chars[j])) {
while (i < j && !Character.isLetter(cs[j])) {
--j;
}
if (i < j) {
char t = chars[i];
chars[i] = chars[j];
chars[j] = t;
char t = cs[i];
cs[i] = cs[j];
cs[j] = t;
++i;
--j;
}
}
return new String(chars);
return new String(cs);
}
}
```
Expand All @@ -112,13 +116,15 @@ public:
string reverseOnlyLetters(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
while (i < j && !isalpha(s[i])) ++i;
while (i < j && !isalpha(s[j])) --j;
if (i < j) {
swap(s[i], s[j]);
while (i < j && !isalpha(s[i])) {
++i;
}
while (i < j && !isalpha(s[j])) {
--j;
}
if (i < j) {
swap(s[i++], s[j--]);
}
}
return s;
}
Expand All @@ -127,39 +133,41 @@ public:

```go
func reverseOnlyLetters(s string) string {
ans := []rune(s)
cs := []rune(s)
i, j := 0, len(s)-1
for i < j {
for i < j && !unicode.IsLetter(ans[i]) {
for i < j && !unicode.IsLetter(cs[i]) {
i++
}
for i < j && !unicode.IsLetter(ans[j]) {
for i < j && !unicode.IsLetter(cs[j]) {
j--
}
if i < j {
ans[i], ans[j] = ans[j], ans[i]
cs[i], cs[j] = cs[j], cs[i]
i++
j--
}
}
return string(ans)
return string(cs)
}
```

```ts
function reverseOnlyLetters(s: string): string {
const n = s.length;
let i = 0,
j = n - 1;
let ans = [...s];
const cs = [...s];
let [i, j] = [0, cs.length - 1];
while (i < j) {
while (!/[a-zA-Z]/.test(ans[i]) && i < j) i++;
while (!/[a-zA-Z]/.test(ans[j]) && i < j) j--;
[ans[i], ans[j]] = [ans[j], ans[i]];
while (!/[a-zA-Z]/.test(cs[i]) && i < j) {
i++;
}
while (!/[a-zA-Z]/.test(cs[j]) && i < j) {
j--;
}
[cs[i], cs[j]] = [cs[j], cs[i]];
i++;
j--;
}
return ans.join('');
return cs.join('');
}
```

Expand Down
72 changes: 40 additions & 32 deletions solution/0900-0999/0917.Reverse Only Letters/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,51 @@

## Solutions

### Solution 1
### Solution 1: Two Pointers

We use two pointers $i$ and $j$ to point to the head and tail of the string respectively. When $i < j$, we continuously move $i$ and $j$ until $i$ points to an English letter and $j$ points to an English letter, then we swap $s[i]$ and $s[j]$. Finally, we return the string.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string.

<!-- tabs:start -->

```python
class Solution:
def reverseOnlyLetters(self, s: str) -> str:
s = list(s)
i, j = 0, len(s) - 1
cs = list(s)
i, j = 0, len(cs) - 1
while i < j:
while i < j and not s[i].isalpha():
while i < j and not cs[i].isalpha():
i += 1
while i < j and not s[j].isalpha():
while i < j and not cs[j].isalpha():
j -= 1
if i < j:
s[i], s[j] = s[j], s[i]
cs[i], cs[j] = cs[j], cs[i]
i, j = i + 1, j - 1
return ''.join(s)
return "".join(cs)
```

```java
class Solution {
public String reverseOnlyLetters(String s) {
char[] chars = s.toCharArray();
int i = 0, j = s.length() - 1;
char[] cs = s.toCharArray();
int i = 0, j = cs.length - 1;
while (i < j) {
while (i < j && !Character.isLetter(chars[i])) {
while (i < j && !Character.isLetter(cs[i])) {
++i;
}
while (i < j && !Character.isLetter(chars[j])) {
while (i < j && !Character.isLetter(cs[j])) {
--j;
}
if (i < j) {
char t = chars[i];
chars[i] = chars[j];
chars[j] = t;
char t = cs[i];
cs[i] = cs[j];
cs[j] = t;
++i;
--j;
}
}
return new String(chars);
return new String(cs);
}
}
```
Expand All @@ -88,13 +92,15 @@ public:
string reverseOnlyLetters(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
while (i < j && !isalpha(s[i])) ++i;
while (i < j && !isalpha(s[j])) --j;
if (i < j) {
swap(s[i], s[j]);
while (i < j && !isalpha(s[i])) {
++i;
}
while (i < j && !isalpha(s[j])) {
--j;
}
if (i < j) {
swap(s[i++], s[j--]);
}
}
return s;
}
Expand All @@ -103,39 +109,41 @@ public:

```go
func reverseOnlyLetters(s string) string {
ans := []rune(s)
cs := []rune(s)
i, j := 0, len(s)-1
for i < j {
for i < j && !unicode.IsLetter(ans[i]) {
for i < j && !unicode.IsLetter(cs[i]) {
i++
}
for i < j && !unicode.IsLetter(ans[j]) {
for i < j && !unicode.IsLetter(cs[j]) {
j--
}
if i < j {
ans[i], ans[j] = ans[j], ans[i]
cs[i], cs[j] = cs[j], cs[i]
i++
j--
}
}
return string(ans)
return string(cs)
}
```

```ts
function reverseOnlyLetters(s: string): string {
const n = s.length;
let i = 0,
j = n - 1;
let ans = [...s];
const cs = [...s];
let [i, j] = [0, cs.length - 1];
while (i < j) {
while (!/[a-zA-Z]/.test(ans[i]) && i < j) i++;
while (!/[a-zA-Z]/.test(ans[j]) && i < j) j--;
[ans[i], ans[j]] = [ans[j], ans[i]];
while (!/[a-zA-Z]/.test(cs[i]) && i < j) {
i++;
}
while (!/[a-zA-Z]/.test(cs[j]) && i < j) {
j--;
}
[cs[i], cs[j]] = [cs[j], cs[i]];
i++;
j--;
}
return ans.join('');
return cs.join('');
}
```

Expand Down
10 changes: 6 additions & 4 deletions solution/0900-0999/0917.Reverse Only Letters/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ class Solution {
string reverseOnlyLetters(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
while (i < j && !isalpha(s[i])) ++i;
while (i < j && !isalpha(s[j])) --j;
if (i < j) {
swap(s[i], s[j]);
while (i < j && !isalpha(s[i])) {
++i;
}
while (i < j && !isalpha(s[j])) {
--j;
}
if (i < j) {
swap(s[i++], s[j--]);
}
}
return s;
}
Expand Down
Loading
Loading