Skip to content

Commit 51b23e9

Browse files
authored
feat: add solutions to lc problems (#2601)
1 parent 20413c4 commit 51b23e9

29 files changed

+503
-355
lines changed

solution/0900-0999/0908.Smallest Range I/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ func smallestRangeI(nums []int, k int) int {
104104

105105
```ts
106106
function smallestRangeI(nums: number[], k: number): number {
107-
const max = nums.reduce((r, v) => Math.max(r, v));
108-
const min = nums.reduce((r, v) => Math.min(r, v));
109-
return Math.max(max - min - k * 2, 0);
107+
const mx = Math.max(...nums);
108+
const mi = Math.min(...nums);
109+
return Math.max(mx - mi - k * 2, 0);
110110
}
111111
```
112112

solution/0900-0999/0908.Smallest Range I/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ func smallestRangeI(nums []int, k int) int {
100100

101101
```ts
102102
function smallestRangeI(nums: number[], k: number): number {
103-
const max = nums.reduce((r, v) => Math.max(r, v));
104-
const min = nums.reduce((r, v) => Math.min(r, v));
105-
return Math.max(max - min - k * 2, 0);
103+
const mx = Math.max(...nums);
104+
const mi = Math.min(...nums);
105+
return Math.max(mx - mi - k * 2, 0);
106106
}
107107
```
108108

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function smallestRangeI(nums: number[], k: number): number {
2-
const max = nums.reduce((r, v) => Math.max(r, v));
3-
const min = nums.reduce((r, v) => Math.min(r, v));
4-
return Math.max(max - min - k * 2, 0);
2+
const mx = Math.max(...nums);
3+
const mi = Math.min(...nums);
4+
return Math.max(mx - mi - k * 2, 0);
55
}

solution/0900-0999/0910.Smallest Range II/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ func smallestRangeII(nums []int, k int) int {
126126
}
127127
```
128128

129+
```ts
130+
function smallestRangeII(nums: number[], k: number): number {
131+
nums.sort((a, b) => a - b);
132+
let ans = nums.at(-1)! - nums[0];
133+
for (let i = 1; i < nums.length; ++i) {
134+
const mi = Math.min(nums[0] + k, nums[i] - k);
135+
const mx = Math.max(nums.at(-1)! - k, nums[i - 1] + k);
136+
ans = Math.min(ans, mx - mi);
137+
}
138+
return ans;
139+
}
140+
```
141+
129142
<!-- tabs:end -->
130143

131144
<!-- end -->

solution/0900-0999/0910.Smallest Range II/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ func smallestRangeII(nums []int, k int) int {
119119
}
120120
```
121121

122+
```ts
123+
function smallestRangeII(nums: number[], k: number): number {
124+
nums.sort((a, b) => a - b);
125+
let ans = nums.at(-1)! - nums[0];
126+
for (let i = 1; i < nums.length; ++i) {
127+
const mi = Math.min(nums[0] + k, nums[i] - k);
128+
const mx = Math.max(nums.at(-1)! - k, nums[i - 1] + k);
129+
ans = Math.min(ans, mx - mi);
130+
}
131+
return ans;
132+
}
133+
```
134+
122135
<!-- tabs:end -->
123136

124137
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function smallestRangeII(nums: number[], k: number): number {
2+
nums.sort((a, b) => a - b);
3+
let ans = nums.at(-1)! - nums[0];
4+
for (let i = 1; i < nums.length; ++i) {
5+
const mi = Math.min(nums[0] + k, nums[i] - k);
6+
const mx = Math.max(nums.at(-1)! - k, nums[i - 1] + k);
7+
ans = Math.min(ans, mx - mi);
8+
}
9+
return ans;
10+
}

solution/0900-0999/0917.Reverse Only Letters/README.md

+40-32
Original file line numberDiff line numberDiff line change
@@ -61,47 +61,51 @@
6161

6262
## 解法
6363

64-
### 方法一
64+
### 方法一:双指针
65+
66+
我们用两个指针 $i$ 和 $j$ 分别指向字符串的头部和尾部。当 $i < j$ 时,我们不断地移动 $i$ 和 $j$,直到 $i$ 指向一个英文字母,并且 $j$ 指向一个英文字母,然后交换 $s[i]$ 和 $s[j]$。最后返回字符串即可。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
6569

6670
<!-- tabs:start -->
6771

6872
```python
6973
class Solution:
7074
def reverseOnlyLetters(self, s: str) -> str:
71-
s = list(s)
72-
i, j = 0, len(s) - 1
75+
cs = list(s)
76+
i, j = 0, len(cs) - 1
7377
while i < j:
74-
while i < j and not s[i].isalpha():
78+
while i < j and not cs[i].isalpha():
7579
i += 1
76-
while i < j and not s[j].isalpha():
80+
while i < j and not cs[j].isalpha():
7781
j -= 1
7882
if i < j:
79-
s[i], s[j] = s[j], s[i]
83+
cs[i], cs[j] = cs[j], cs[i]
8084
i, j = i + 1, j - 1
81-
return ''.join(s)
85+
return "".join(cs)
8286
```
8387

8488
```java
8589
class Solution {
8690
public String reverseOnlyLetters(String s) {
87-
char[] chars = s.toCharArray();
88-
int i = 0, j = s.length() - 1;
91+
char[] cs = s.toCharArray();
92+
int i = 0, j = cs.length - 1;
8993
while (i < j) {
90-
while (i < j && !Character.isLetter(chars[i])) {
94+
while (i < j && !Character.isLetter(cs[i])) {
9195
++i;
9296
}
93-
while (i < j && !Character.isLetter(chars[j])) {
97+
while (i < j && !Character.isLetter(cs[j])) {
9498
--j;
9599
}
96100
if (i < j) {
97-
char t = chars[i];
98-
chars[i] = chars[j];
99-
chars[j] = t;
101+
char t = cs[i];
102+
cs[i] = cs[j];
103+
cs[j] = t;
100104
++i;
101105
--j;
102106
}
103107
}
104-
return new String(chars);
108+
return new String(cs);
105109
}
106110
}
107111
```
@@ -112,13 +116,15 @@ public:
112116
string reverseOnlyLetters(string s) {
113117
int i = 0, j = s.size() - 1;
114118
while (i < j) {
115-
while (i < j && !isalpha(s[i])) ++i;
116-
while (i < j && !isalpha(s[j])) --j;
117-
if (i < j) {
118-
swap(s[i], s[j]);
119+
while (i < j && !isalpha(s[i])) {
119120
++i;
121+
}
122+
while (i < j && !isalpha(s[j])) {
120123
--j;
121124
}
125+
if (i < j) {
126+
swap(s[i++], s[j--]);
127+
}
122128
}
123129
return s;
124130
}
@@ -127,39 +133,41 @@ public:
127133
128134
```go
129135
func reverseOnlyLetters(s string) string {
130-
ans := []rune(s)
136+
cs := []rune(s)
131137
i, j := 0, len(s)-1
132138
for i < j {
133-
for i < j && !unicode.IsLetter(ans[i]) {
139+
for i < j && !unicode.IsLetter(cs[i]) {
134140
i++
135141
}
136-
for i < j && !unicode.IsLetter(ans[j]) {
142+
for i < j && !unicode.IsLetter(cs[j]) {
137143
j--
138144
}
139145
if i < j {
140-
ans[i], ans[j] = ans[j], ans[i]
146+
cs[i], cs[j] = cs[j], cs[i]
141147
i++
142148
j--
143149
}
144150
}
145-
return string(ans)
151+
return string(cs)
146152
}
147153
```
148154

149155
```ts
150156
function reverseOnlyLetters(s: string): string {
151-
const n = s.length;
152-
let i = 0,
153-
j = n - 1;
154-
let ans = [...s];
157+
const cs = [...s];
158+
let [i, j] = [0, cs.length - 1];
155159
while (i < j) {
156-
while (!/[a-zA-Z]/.test(ans[i]) && i < j) i++;
157-
while (!/[a-zA-Z]/.test(ans[j]) && i < j) j--;
158-
[ans[i], ans[j]] = [ans[j], ans[i]];
160+
while (!/[a-zA-Z]/.test(cs[i]) && i < j) {
161+
i++;
162+
}
163+
while (!/[a-zA-Z]/.test(cs[j]) && i < j) {
164+
j--;
165+
}
166+
[cs[i], cs[j]] = [cs[j], cs[i]];
159167
i++;
160168
j--;
161169
}
162-
return ans.join('');
170+
return cs.join('');
163171
}
164172
```
165173

solution/0900-0999/0917.Reverse Only Letters/README_EN.md

+40-32
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,51 @@
3737

3838
## Solutions
3939

40-
### Solution 1
40+
### Solution 1: Two Pointers
41+
42+
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.
43+
44+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string.
4145

4246
<!-- tabs:start -->
4347

4448
```python
4549
class Solution:
4650
def reverseOnlyLetters(self, s: str) -> str:
47-
s = list(s)
48-
i, j = 0, len(s) - 1
51+
cs = list(s)
52+
i, j = 0, len(cs) - 1
4953
while i < j:
50-
while i < j and not s[i].isalpha():
54+
while i < j and not cs[i].isalpha():
5155
i += 1
52-
while i < j and not s[j].isalpha():
56+
while i < j and not cs[j].isalpha():
5357
j -= 1
5458
if i < j:
55-
s[i], s[j] = s[j], s[i]
59+
cs[i], cs[j] = cs[j], cs[i]
5660
i, j = i + 1, j - 1
57-
return ''.join(s)
61+
return "".join(cs)
5862
```
5963

6064
```java
6165
class Solution {
6266
public String reverseOnlyLetters(String s) {
63-
char[] chars = s.toCharArray();
64-
int i = 0, j = s.length() - 1;
67+
char[] cs = s.toCharArray();
68+
int i = 0, j = cs.length - 1;
6569
while (i < j) {
66-
while (i < j && !Character.isLetter(chars[i])) {
70+
while (i < j && !Character.isLetter(cs[i])) {
6771
++i;
6872
}
69-
while (i < j && !Character.isLetter(chars[j])) {
73+
while (i < j && !Character.isLetter(cs[j])) {
7074
--j;
7175
}
7276
if (i < j) {
73-
char t = chars[i];
74-
chars[i] = chars[j];
75-
chars[j] = t;
77+
char t = cs[i];
78+
cs[i] = cs[j];
79+
cs[j] = t;
7680
++i;
7781
--j;
7882
}
7983
}
80-
return new String(chars);
84+
return new String(cs);
8185
}
8286
}
8387
```
@@ -88,13 +92,15 @@ public:
8892
string reverseOnlyLetters(string s) {
8993
int i = 0, j = s.size() - 1;
9094
while (i < j) {
91-
while (i < j && !isalpha(s[i])) ++i;
92-
while (i < j && !isalpha(s[j])) --j;
93-
if (i < j) {
94-
swap(s[i], s[j]);
95+
while (i < j && !isalpha(s[i])) {
9596
++i;
97+
}
98+
while (i < j && !isalpha(s[j])) {
9699
--j;
97100
}
101+
if (i < j) {
102+
swap(s[i++], s[j--]);
103+
}
98104
}
99105
return s;
100106
}
@@ -103,39 +109,41 @@ public:
103109
104110
```go
105111
func reverseOnlyLetters(s string) string {
106-
ans := []rune(s)
112+
cs := []rune(s)
107113
i, j := 0, len(s)-1
108114
for i < j {
109-
for i < j && !unicode.IsLetter(ans[i]) {
115+
for i < j && !unicode.IsLetter(cs[i]) {
110116
i++
111117
}
112-
for i < j && !unicode.IsLetter(ans[j]) {
118+
for i < j && !unicode.IsLetter(cs[j]) {
113119
j--
114120
}
115121
if i < j {
116-
ans[i], ans[j] = ans[j], ans[i]
122+
cs[i], cs[j] = cs[j], cs[i]
117123
i++
118124
j--
119125
}
120126
}
121-
return string(ans)
127+
return string(cs)
122128
}
123129
```
124130

125131
```ts
126132
function reverseOnlyLetters(s: string): string {
127-
const n = s.length;
128-
let i = 0,
129-
j = n - 1;
130-
let ans = [...s];
133+
const cs = [...s];
134+
let [i, j] = [0, cs.length - 1];
131135
while (i < j) {
132-
while (!/[a-zA-Z]/.test(ans[i]) && i < j) i++;
133-
while (!/[a-zA-Z]/.test(ans[j]) && i < j) j--;
134-
[ans[i], ans[j]] = [ans[j], ans[i]];
136+
while (!/[a-zA-Z]/.test(cs[i]) && i < j) {
137+
i++;
138+
}
139+
while (!/[a-zA-Z]/.test(cs[j]) && i < j) {
140+
j--;
141+
}
142+
[cs[i], cs[j]] = [cs[j], cs[i]];
135143
i++;
136144
j--;
137145
}
138-
return ans.join('');
146+
return cs.join('');
139147
}
140148
```
141149

solution/0900-0999/0917.Reverse Only Letters/Solution.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ class Solution {
33
string reverseOnlyLetters(string s) {
44
int i = 0, j = s.size() - 1;
55
while (i < j) {
6-
while (i < j && !isalpha(s[i])) ++i;
7-
while (i < j && !isalpha(s[j])) --j;
8-
if (i < j) {
9-
swap(s[i], s[j]);
6+
while (i < j && !isalpha(s[i])) {
107
++i;
8+
}
9+
while (i < j && !isalpha(s[j])) {
1110
--j;
1211
}
12+
if (i < j) {
13+
swap(s[i++], s[j--]);
14+
}
1315
}
1416
return s;
1517
}

0 commit comments

Comments
 (0)