46
46
47
47
<!-- 这里可写通用的实现逻辑 -->
48
48
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)$。
50
57
51
58
<!-- tabs:start -->
52
59
57
64
``` python
58
65
class Solution :
59
66
def validPalindrome (self , s : str ) -> bool :
60
- def check (i , j ) :
67
+ def check (i : int , j : int ) -> bool :
61
68
while i < j:
62
69
if s[i] != s[j]:
63
70
return False
@@ -67,7 +74,7 @@ class Solution:
67
74
i, j = 0 , len (s) - 1
68
75
while i < j:
69
76
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 )
71
78
i, j = i + 1 , j - 1
72
79
return True
73
80
```
@@ -78,16 +85,19 @@ class Solution:
78
85
79
86
``` java
80
87
class Solution {
88
+ private String s;
89
+
81
90
public boolean validPalindrome (String s ) {
91
+ this . s = s;
82
92
for (int i = 0 , j = s. length() - 1 ; i < j; ++ i, -- j) {
83
93
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 );
85
95
}
86
96
}
87
97
return true ;
88
98
}
89
99
90
- private boolean check (String s , int i , int j ) {
100
+ private boolean check (int i , int j ) {
91
101
for (; i < j; ++ i, -- j) {
92
102
if (s. charAt(i) != s. charAt(j)) {
93
103
return false ;
@@ -98,48 +108,26 @@ class Solution {
98
108
}
99
109
```
100
110
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
111
### ** C++**
127
112
128
113
``` cpp
129
114
class Solution {
130
115
public:
131
116
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;
143
131
}
144
132
};
145
133
```
@@ -165,6 +153,27 @@ func validPalindrome(s string) bool {
165
153
}
166
154
```
167
155
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
+
168
177
### ** JavaScript**
169
178
170
179
``` js
@@ -173,16 +182,16 @@ func validPalindrome(s string) bool {
173
182
* @return {boolean}
174
183
*/
175
184
var validPalindrome = function (s ) {
176
- let check = function (i , j ) {
185
+ const check = (i , j ) => {
177
186
for (; i < j; ++ i, -- j) {
178
- if (s . charAt (i) != s . charAt (j) ) {
187
+ if (s[i] !== s[j] ) {
179
188
return false ;
180
189
}
181
190
}
182
191
return true ;
183
192
};
184
193
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] ) {
186
195
return check (i + 1 , j) || check (i, j - 1 );
187
196
}
188
197
}
0 commit comments