File tree 3 files changed +49
-40
lines changed
solution/0100-0199/0125.Valid Palindrome 3 files changed +49
-40
lines changed Original file line number Diff line number Diff line change @@ -140,7 +140,11 @@ public class Solution {
140
140
### ** JavaScript**
141
141
142
142
``` js
143
- const isPalindrome1 = function (s ) {
143
+ /**
144
+ * @param {string} s
145
+ * @return {boolean}
146
+ */
147
+ var isPalindrome = function (s ) {
144
148
let arr1 = [],
145
149
arr2 = [];
146
150
for (let i = 0 ; i < s .length ; i++ ) {
@@ -155,8 +159,14 @@ const isPalindrome1 = function (s) {
155
159
arr2 .reverse ();
156
160
return arr1 .join (' ' ) === arr2 .join (' ' );
157
161
};
162
+ ```
158
163
159
- const isPalindrome = function (s ) {
164
+ ``` js
165
+ /**
166
+ * @param {string} s
167
+ * @return {boolean}
168
+ */
169
+ var isPalindrome = function (s ) {
160
170
function isNumOrAl (a ) {
161
171
if (
162
172
(a >= ' A' && a <= ' Z' ) ||
@@ -216,6 +226,16 @@ function isPalindrome(s: string): boolean {
216
226
}
217
227
```
218
228
229
+ ``` ts
230
+ function isPalindrome(s : string ): boolean {
231
+ const isAlphanumeric = (c : string ) => {
232
+ return (c >= ' a' && c <= ' z' ) || (c >= ' 0' && c <= ' 9' );
233
+ };
234
+ const cs = s .toLocaleLowerCase ().split (' ' ).filter (isAlphanumeric );
235
+ return cs .join (' ' ) === cs .reverse ().join (' ' );
236
+ }
237
+ ```
238
+
219
239
### ** Rust**
220
240
221
241
``` rust
Original file line number Diff line number Diff line change @@ -139,7 +139,11 @@ public class Solution {
139
139
### ** JavaScript**
140
140
141
141
``` js
142
- const isPalindrome1 = function (s ) {
142
+ /**
143
+ * @param {string} s
144
+ * @return {boolean}
145
+ */
146
+ var isPalindrome = function (s ) {
143
147
let arr1 = [],
144
148
arr2 = [];
145
149
for (let i = 0 ; i < s .length ; i++ ) {
@@ -154,8 +158,14 @@ const isPalindrome1 = function (s) {
154
158
arr2 .reverse ();
155
159
return arr1 .join (' ' ) === arr2 .join (' ' );
156
160
};
161
+ ```
157
162
158
- const isPalindrome = function (s ) {
163
+ ``` js
164
+ /**
165
+ * @param {string} s
166
+ * @return {boolean}
167
+ */
168
+ var isPalindrome = function (s ) {
159
169
function isNumOrAl (a ) {
160
170
if (
161
171
(a >= ' A' && a <= ' Z' ) ||
@@ -215,6 +225,16 @@ function isPalindrome(s: string): boolean {
215
225
}
216
226
```
217
227
228
+ ``` ts
229
+ function isPalindrome(s : string ): boolean {
230
+ const isAlphanumeric = (c : string ) => {
231
+ return (c >= ' a' && c <= ' z' ) || (c >= ' 0' && c <= ' 9' );
232
+ };
233
+ const cs = s .toLocaleLowerCase ().split (' ' ).filter (isAlphanumeric );
234
+ return cs .join (' ' ) === cs .reverse ().join (' ' );
235
+ }
236
+ ```
237
+
218
238
### ** Rust**
219
239
220
240
``` rust
Original file line number Diff line number Diff line change 1
- const isPalindrome1 = function ( s ) {
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var isPalindrome = function ( s ) {
2
6
let arr1 = [ ] ,
3
7
arr2 = [ ] ;
4
8
for ( let i = 0 ; i < s . length ; i ++ ) {
@@ -13,38 +17,3 @@ const isPalindrome1 = function (s) {
13
17
arr2 . reverse ( ) ;
14
18
return arr1 . join ( '' ) === arr2 . join ( '' ) ;
15
19
} ;
16
-
17
- const isPalindrome = function ( s ) {
18
- function isNumOrAl ( a ) {
19
- if (
20
- ( a >= 'A' && a <= 'Z' ) ||
21
- ( a >= '0' && a <= '9' ) ||
22
- ( a >= 'a' && a <= 'z' )
23
- ) {
24
- return true ;
25
- } else {
26
- return false ;
27
- }
28
- }
29
-
30
- if ( s . length === 0 ) {
31
- return true ;
32
- }
33
- let i = 0 ,
34
- j = s . length - 1 ;
35
- while ( i < j ) {
36
- while ( i < j && ! isNumOrAl ( s [ i ] ) ) {
37
- i ++ ;
38
- }
39
- while ( i < j && ! isNumOrAl ( s [ j ] ) ) {
40
- j -- ;
41
- }
42
- if ( s [ i ] . toLowerCase ( ) !== s [ j ] . toLowerCase ( ) ) {
43
- return false ;
44
- } else {
45
- i ++ ;
46
- j -- ;
47
- }
48
- }
49
- return true ;
50
- } ;
You can’t perform that action at this time.
0 commit comments