Skip to content

Commit d03d301

Browse files
authoredAug 27, 2023
Merge branch 'main' into feat-0084
2 parents 95a1066 + 22cc6fb commit d03d301

File tree

199 files changed

+7879
-985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+7879
-985
lines changed
 

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

+53-44
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@
4646

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

49-
双指针,当 `s[i]` 不等于 `s[j]` 时,分别尝试跳过 `i` 或跳过 `j`
49+
**方法一:双指针**
50+
51+
我们用两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 的第一个字符和最后一个字符,然后向中间移动指针,每次判断 $s[i]$ 和 $s[j]$ 是否相等:
52+
53+
- 如果 $s[i] = s[j]$,则指针 $i$ 向后移动一位,指针 $j$ 向前移动一位;
54+
- 否则,存在两种情况,即删除字符 $s[i]$ 或者删除字符 $s[j]$,然后判断删除之后的字符串是否是回文字符串。即判断子串 $s[i+1..j]$ 或者子串 $s[i..j-1]$ 是否是回文字符串。
55+
56+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
5057

5158
<!-- tabs:start -->
5259

@@ -57,7 +64,7 @@
5764
```python
5865
class Solution:
5966
def validPalindrome(self, s: str) -> bool:
60-
def check(i, j):
67+
def check(i: int, j: int) -> bool:
6168
while i < j:
6269
if s[i] != s[j]:
6370
return False
@@ -67,7 +74,7 @@ class Solution:
6774
i, j = 0, len(s) - 1
6875
while i < j:
6976
if s[i] != s[j]:
70-
return check(i, j - 1) or check(i + 1, j)
77+
return check(i + 1, j) or check(i, j - 1)
7178
i, j = i + 1, j - 1
7279
return True
7380
```
@@ -78,16 +85,19 @@ class Solution:
7885

7986
```java
8087
class Solution {
88+
private String s;
89+
8190
public boolean validPalindrome(String s) {
91+
this.s = s;
8292
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
8393
if (s.charAt(i) != s.charAt(j)) {
84-
return check(s, i + 1, j) || check(s, i, j - 1);
94+
return check(i + 1, j) || check(i, j - 1);
8595
}
8696
}
8797
return true;
8898
}
8999

90-
private boolean check(String s, int i, int j) {
100+
private boolean check(int i, int j) {
91101
for (; i < j; ++i, --j) {
92102
if (s.charAt(i) != s.charAt(j)) {
93103
return false;
@@ -98,48 +108,26 @@ class Solution {
98108
}
99109
```
100110

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-
126111
### **C++**
127112

128113
```cpp
129114
class Solution {
130115
public:
131116
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;
117+
auto check = [&](int i, int j) {
118+
for (; i < j; ++i, --j) {
119+
if (s[i] != s[j]) {
120+
return false;
121+
}
122+
}
123+
return true;
124+
};
125+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
126+
if (s[i] != s[j]) {
127+
return check(i + 1, j) || check(i, j - 1);
128+
}
129+
}
130+
return true;
143131
}
144132
};
145133
```
@@ -165,6 +153,27 @@ func validPalindrome(s string) bool {
165153
}
166154
```
167155

156+
### **TypeScript**
157+
158+
```ts
159+
function validPalindrome(s: string): boolean {
160+
const check = (i: number, j: number): boolean => {
161+
for (; i < j; ++i, --j) {
162+
if (s[i] !== s[j]) {
163+
return false;
164+
}
165+
}
166+
return true;
167+
};
168+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
169+
if (s[i] !== s[j]) {
170+
return check(i + 1, j) || check(i, j - 1);
171+
}
172+
}
173+
return true;
174+
}
175+
```
176+
168177
### **JavaScript**
169178

170179
```js
@@ -173,16 +182,16 @@ func validPalindrome(s string) bool {
173182
* @return {boolean}
174183
*/
175184
var validPalindrome = function (s) {
176-
let check = function (i, j) {
185+
const check = (i, j) => {
177186
for (; i < j; ++i, --j) {
178-
if (s.charAt(i) != s.charAt(j)) {
187+
if (s[i] !== s[j]) {
179188
return false;
180189
}
181190
}
182191
return true;
183192
};
184193
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
185-
if (s.charAt(i) != s.charAt(j)) {
194+
if (s[i] !== s[j]) {
186195
return check(i + 1, j) || check(i, j - 1);
187196
}
188197
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
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-
}
1+
class Solution {
2+
public:
3+
bool validPalindrome(string s) {
4+
auto check = [&](int i, int j) {
5+
for (; i < j; ++i, --j) {
6+
if (s[i] != s[j]) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
};
12+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
13+
if (s[i] != s[j]) {
14+
return check(i + 1, j) || check(i, j - 1);
15+
}
16+
}
17+
return true;
18+
}
1619
};

0 commit comments

Comments
 (0)