Skip to content

Commit b91aa14

Browse files
committed
feat: add solutions to lc/lcof2 problems
lc No.0680 & lcof2 No.019.Valid Palindrome II
1 parent 6432728 commit b91aa14

File tree

20 files changed

+404
-178
lines changed

20 files changed

+404
-178
lines changed

lcof2/剑指 Offer II 019. 最多删除一个字符得到回文/README.md

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

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

49-
双指针,当 `s[i]` 不等于 `s[j]` 时,分别尝试跳过 `i` 或跳过 `j`
49+
双指针,当 `s[i]` 不等于 `s[j]` 时,分别尝试跳过 `i` 或跳过 `j`
5050

5151
<!-- tabs:start -->
5252

@@ -61,17 +61,14 @@ class Solution:
6161
while i < j:
6262
if s[i] != s[j]:
6363
return False
64-
i += 1
65-
j -= 1
64+
i, j = i + 1, j - 1
6665
return True
6766

6867
i, j = 0, len(s) - 1
6968
while i < j:
70-
if s[i] == s[j]:
71-
i += 1
72-
j -= 1
73-
else:
74-
return check(i + 1, j) or check(i, j - 1)
69+
if s[i] != s[j]:
70+
return check(i, j - 1) or check(i + 1, j)
71+
i, j = i + 1, j - 1
7572
return True
7673
```
7774

@@ -82,59 +79,117 @@ class Solution:
8279
```java
8380
class Solution {
8481
public boolean validPalindrome(String s) {
85-
int i = 0, j = s.length() - 1;
86-
while (i < j) {
87-
if (s.charAt(i) == s.charAt(j)) {
88-
i++;
89-
j--;
90-
} else {
82+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
83+
if (s.charAt(i) != s.charAt(j)) {
9184
return check(s, i + 1, j) || check(s, i, j - 1);
9285
}
9386
}
9487
return true;
9588
}
9689

9790
private boolean check(String s, int i, int j) {
98-
while (i < j) {
91+
for (; i < j; ++i, --j) {
9992
if (s.charAt(i) != s.charAt(j)) {
10093
return false;
10194
}
102-
i++;
103-
j--;
10495
}
10596
return true;
10697
}
10798
}
10899
```
109100

101+
### **TypeScript**
102+
103+
```ts
104+
function validPalindrome(s: string): boolean {
105+
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
106+
if (s.charAt(i) != s.charAt(j)) {
107+
return (
108+
isPalinddrome(s.slice(i, j)) ||
109+
isPalinddrome(s.slice(i + 1, j + 1))
110+
);
111+
}
112+
}
113+
return true;
114+
}
115+
116+
function isPalinddrome(s: string): boolean {
117+
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
118+
if (s.charAt(i) != s.charAt(j)) {
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
bool validPalindrome(string s) {
132+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
133+
if (s[i] != s[j])
134+
return check(s, i + 1, j) || check(s, i, j - 1);
135+
return 1;
136+
}
137+
138+
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;
143+
}
144+
};
145+
```
146+
110147
### **Go**
111148

112149
```go
113150
func validPalindrome(s string) bool {
114-
i, j := 0, len(s)-1
115-
for i < j {
116-
if s[i] == s[j] {
117-
i++
118-
j--
119-
} else {
120-
return check(s, i+1, j) || check(s, i, j-1)
151+
check := func(i, j int) bool {
152+
for ; i < j; i, j = i+1, j-1 {
153+
if s[i] != s[j] {
154+
return false
155+
}
121156
}
157+
return true
122158
}
123-
return true
124-
}
125-
126-
func check(s string, i, j int) bool {
127-
for i < j {
159+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
128160
if s[i] != s[j] {
129-
return false
161+
return check(i+1, j) || check(i, j-1)
130162
}
131-
i++
132-
j--
133163
}
134164
return true
135165
}
136166
```
137167

168+
### **JavaScript**
169+
170+
```js
171+
/**
172+
* @param {string} s
173+
* @return {boolean}
174+
*/
175+
var validPalindrome = function (s) {
176+
let check = function (i, j) {
177+
for (; i < j; ++i, --j) {
178+
if (s.charAt(i) != s.charAt(j)) {
179+
return false;
180+
}
181+
}
182+
return true;
183+
};
184+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
185+
if (s.charAt(i) != s.charAt(j)) {
186+
return check(i + 1, j) || check(i, j - 1);
187+
}
188+
}
189+
return true;
190+
};
191+
```
192+
138193
### **...**
139194

140195
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool validPalindrome(string s) {
4+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
5+
if (s[i] != s[j])
6+
return check(s, i + 1, j) || check(s, i, j - 1);
7+
return 1;
8+
}
9+
10+
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;
15+
}
16+
};
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
func validPalindrome(s string) bool {
2-
i, j := 0, len(s)-1
3-
for i < j {
4-
if s[i] == s[j] {
5-
i++
6-
j--
7-
} else {
8-
return check(s, i+1, j) || check(s, i, j-1)
2+
check := func(i, j int) bool {
3+
for ; i < j; i, j = i+1, j-1 {
4+
if s[i] != s[j] {
5+
return false
6+
}
97
}
8+
return true
109
}
11-
return true
12-
}
13-
14-
func check(s string, i, j int) bool {
15-
for i < j {
10+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
1611
if s[i] != s[j] {
17-
return false
12+
return check(i+1, j) || check(i, j-1)
1813
}
19-
i++
20-
j--
2114
}
2215
return true
23-
}
16+
}
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
class Solution {
22
public boolean validPalindrome(String s) {
3-
int i = 0, j = s.length() - 1;
4-
while (i < j) {
5-
if (s.charAt(i) == s.charAt(j)) {
6-
i++;
7-
j--;
8-
} else {
3+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
4+
if (s.charAt(i) != s.charAt(j)) {
95
return check(s, i + 1, j) || check(s, i, j - 1);
106
}
117
}
128
return true;
139
}
1410

1511
private boolean check(String s, int i, int j) {
16-
while (i < j) {
12+
for (; i < j; ++i, --j) {
1713
if (s.charAt(i) != s.charAt(j)) {
1814
return false;
1915
}
20-
i++;
21-
j--;
2216
}
2317
return true;
2418
}
25-
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var validPalindrome = function (s) {
6+
let check = function (i, j) {
7+
for (; i < j; ++i, --j) {
8+
if (s.charAt(i) != s.charAt(j)) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
};
14+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
15+
if (s.charAt(i) != s.charAt(j)) {
16+
return check(i + 1, j) || check(i, j - 1);
17+
}
18+
}
19+
return true;
20+
};

lcof2/剑指 Offer II 019. 最多删除一个字符得到回文/Solution.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ def check(i, j):
44
while i < j:
55
if s[i] != s[j]:
66
return False
7-
i += 1
8-
j -= 1
7+
i, j = i + 1, j - 1
98
return True
109

1110
i, j = 0, len(s) - 1
1211
while i < j:
13-
if s[i] == s[j]:
14-
i += 1
15-
j -= 1
16-
else:
17-
return check(i + 1, j) or check(i, j - 1)
12+
if s[i] != s[j]:
13+
return check(i, j - 1) or check(i + 1, j)
14+
i, j = i + 1, j - 1
1815
return True
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function validPalindrome(s: string): boolean {
2+
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
3+
if (s.charAt(i) != s.charAt(j)) {
4+
return (
5+
isPalinddrome(s.slice(i, j)) ||
6+
isPalinddrome(s.slice(i + 1, j + 1))
7+
);
8+
}
9+
}
10+
return true;
11+
}
12+
13+
function isPalinddrome(s: string): boolean {
14+
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
15+
if (s.charAt(i) != s.charAt(j)) {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}

0 commit comments

Comments
 (0)