Skip to content

Commit 574b91f

Browse files
authored
feat: add solutions to lc problem: No.0680 (doocs#1347)
No.0680.Valid Palindrome II
1 parent 2cfd23b commit 574b91f

File tree

8 files changed

+123
-23
lines changed

8 files changed

+123
-23
lines changed

solution/0600-0699/0680.Valid Palindrome II/README.md

+46-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@
4646

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

49-
双指针,当 `s[i]` 不等于 `s[j]` 时,分别尝试跳过 `i` 或跳过 `j`
49+
**方法一:双指针**
50+
51+
我们用两个指针分别指向字符串的左右两端,每次判断两个指针指向的字符是否相同,如果不相同,则判断删除左指针对应的字符后字符串是否是回文字符串,或者判断删除右指针对应的字符后字符串是否是回文字符串。如果两个指针指向的字符相同,则将左右指针都往中间移动一位,直到两个指针相遇为止。
52+
53+
如果遍历结束,都没有遇到指针指向的字符不相同的情况,那么字符串本身就是一个回文字符串,返回 `true` 即可。
54+
55+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
5056

5157
<!-- tabs:start -->
5258

@@ -129,17 +135,21 @@ function isPalinddrome(s: string): boolean {
129135
class Solution {
130136
public:
131137
bool validPalindrome(string s) {
132-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
133-
if (s[i] != s[j])
138+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
139+
if (s[i] != s[j]) {
134140
return check(s, i + 1, j) || check(s, i, j - 1);
141+
}
142+
}
135143
return 1;
136144
}
137145

138146
bool check(string s, int i, int j) {
139-
for (; i < j; ++i, --j)
140-
if (s[i] != s[j])
141-
return 0;
142-
return 1;
147+
for (; i < j; ++i, --j) {
148+
if (s[i] != s[j]) {
149+
return false;
150+
}
151+
}
152+
return true;
143153
}
144154
};
145155
```
@@ -190,6 +200,35 @@ var validPalindrome = function (s) {
190200
};
191201
```
192202

203+
### **C#**
204+
205+
```cs
206+
public class Solution {
207+
public bool ValidPalindrome(string s) {
208+
int i = 0, j = s.Length - 1;
209+
while (i < j && s[i] == s[j]) {
210+
i++;
211+
j--;
212+
}
213+
if (i >= j) {
214+
return true;
215+
}
216+
return check(s, i + 1, j) || check(s, i, j - 1);
217+
}
218+
219+
private bool check(string s, int i, int j) {
220+
while (i < j) {
221+
if (s[i] != s[j]) {
222+
return false;
223+
}
224+
i++;
225+
j--;
226+
}
227+
return true;
228+
}
229+
}
230+
```
231+
193232
### **...**
194233

195234
```

solution/0600-0699/0680.Valid Palindrome II/README_EN.md

+39-6
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,21 @@ function isPalinddrome(s: string): boolean {
116116
class Solution {
117117
public:
118118
bool validPalindrome(string s) {
119-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
120-
if (s[i] != s[j])
119+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
120+
if (s[i] != s[j]) {
121121
return check(s, i + 1, j) || check(s, i, j - 1);
122+
}
123+
}
122124
return 1;
123125
}
124126

125127
bool check(string s, int i, int j) {
126-
for (; i < j; ++i, --j)
127-
if (s[i] != s[j])
128-
return 0;
129-
return 1;
128+
for (; i < j; ++i, --j) {
129+
if (s[i] != s[j]) {
130+
return false;
131+
}
132+
}
133+
return true;
130134
}
131135
};
132136
```
@@ -177,6 +181,35 @@ var validPalindrome = function (s) {
177181
};
178182
```
179183

184+
### **C#**
185+
186+
```cs
187+
public class Solution {
188+
public bool ValidPalindrome(string s) {
189+
int i = 0, j = s.Length - 1;
190+
while (i < j && s[i] == s[j]) {
191+
i++;
192+
j--;
193+
}
194+
if (i >= j) {
195+
return true;
196+
}
197+
return check(s, i + 1, j) || check(s, i, j - 1);
198+
}
199+
200+
private bool check(string s, int i, int j) {
201+
while (i < j) {
202+
if (s[i] != s[j]) {
203+
return false;
204+
}
205+
i++;
206+
j--;
207+
}
208+
return true;
209+
}
210+
}
211+
```
212+
180213
### **...**
181214

182215
```
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
class Solution {
22
public:
33
bool validPalindrome(string s) {
4-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
5-
if (s[i] != s[j])
4+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
5+
if (s[i] != s[j]) {
66
return check(s, i + 1, j) || check(s, i, j - 1);
7+
}
8+
}
79
return 1;
810
}
911

1012
bool check(string s, int i, int j) {
11-
for (; i < j; ++i, --j)
12-
if (s[i] != s[j])
13-
return 0;
14-
return 1;
13+
for (; i < j; ++i, --j) {
14+
if (s[i] != s[j]) {
15+
return false;
16+
}
17+
}
18+
return true;
1519
}
1620
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public bool ValidPalindrome(string s) {
3+
int i = 0, j = s.Length - 1;
4+
while (i < j && s[i] == s[j]) {
5+
i++;
6+
j--;
7+
}
8+
if (i >= j) {
9+
return true;
10+
}
11+
return check(s, i + 1, j) || check(s, i, j - 1);
12+
}
13+
14+
private bool check(string s, int i, int j) {
15+
while (i < j) {
16+
if (s[i] != s[j]) {
17+
return false;
18+
}
19+
i++;
20+
j--;
21+
}
22+
return true;
23+
}
24+
}

solution/2400-2499/2465.Number of Distinct Averages/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl Solution {
237237
for i in 0..n >> 1 {
238238
let x = (nums[i] + nums[n - i - 1]) as usize;
239239
cnt[x] += 1;
240-
240+
241241
if cnt[x] == 1 {
242242
ans += 1;
243243
}

solution/2400-2499/2465.Number of Distinct Averages/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl Solution {
223223
for i in 0..n >> 1 {
224224
let x = (nums[i] + nums[n - i - 1]) as usize;
225225
cnt[x] += 1;
226-
226+
227227
if cnt[x] == 1 {
228228
ans += 1;
229229
}

solution/2400-2499/2475.Number of Unequal Triplets in Array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl Solution {
444444
let k = r;
445445

446446
ans += j * (n - k)
447-
}
447+
}
448448

449449
ans as i32
450450
}

solution/2400-2499/2475.Number of Unequal Triplets in Array/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ impl Solution {
412412
let k = r;
413413

414414
ans += j * (n - k)
415-
}
415+
}
416416

417417
ans as i32
418418
}

0 commit comments

Comments
 (0)