46
46
47
47
<!-- 这里可写通用的实现逻辑 -->
48
48
49
- 双指针,当 ` s[i] ` 不等于 ` s[j] ` 时,分别尝试跳过 ` i ` 或跳过 ` j `
49
+ 双指针,当 ` s[i] ` 不等于 ` s[j] ` 时,分别尝试跳过 ` i ` 或跳过 ` j ` 。
50
50
51
51
<!-- tabs:start -->
52
52
@@ -61,17 +61,14 @@ class Solution:
61
61
while i < j:
62
62
if s[i] != s[j]:
63
63
return False
64
- i += 1
65
- j -= 1
64
+ i, j = i + 1 , j - 1
66
65
return True
67
66
68
67
i, j = 0 , len (s) - 1
69
68
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
75
72
return True
76
73
```
77
74
@@ -82,59 +79,117 @@ class Solution:
82
79
``` java
83
80
class Solution {
84
81
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)) {
91
84
return check(s, i + 1 , j) || check(s, i, j - 1 );
92
85
}
93
86
}
94
87
return true ;
95
88
}
96
89
97
90
private boolean check (String s , int i , int j ) {
98
- while ( i < j) {
91
+ for (; i < j; ++ i, -- j) {
99
92
if (s. charAt(i) != s. charAt(j)) {
100
93
return false ;
101
94
}
102
- i++ ;
103
- j-- ;
104
95
}
105
96
return true ;
106
97
}
107
98
}
108
99
```
109
100
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
+
110
147
### ** Go**
111
148
112
149
``` go
113
150
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
+ }
121
156
}
157
+ return true
122
158
}
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 {
128
160
if s[i] != s[j] {
129
- return false
161
+ return check (i+ 1 , j) || check (i, j- 1 )
130
162
}
131
- i++
132
- j--
133
163
}
134
164
return true
135
165
}
136
166
```
137
167
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
+
138
193
### ** ...**
139
194
140
195
```
0 commit comments