Skip to content

Commit 014178e

Browse files
committedApr 26, 2022
feat: update js solution to lc problem: No.0125
No.0125.Valid Palindrome
1 parent e56ff75 commit 014178e

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed
 

‎solution/0100-0199/0125.Valid Palindrome/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ public class Solution {
140140
### **JavaScript**
141141

142142
```js
143-
const isPalindrome1 = function (s) {
143+
/**
144+
* @param {string} s
145+
* @return {boolean}
146+
*/
147+
var isPalindrome = function (s) {
144148
let arr1 = [],
145149
arr2 = [];
146150
for (let i = 0; i < s.length; i++) {
@@ -155,8 +159,14 @@ const isPalindrome1 = function (s) {
155159
arr2.reverse();
156160
return arr1.join('') === arr2.join('');
157161
};
162+
```
158163

159-
const isPalindrome = function (s) {
164+
```js
165+
/**
166+
* @param {string} s
167+
* @return {boolean}
168+
*/
169+
var isPalindrome = function (s) {
160170
function isNumOrAl(a) {
161171
if (
162172
(a >= 'A' && a <= 'Z') ||
@@ -216,6 +226,16 @@ function isPalindrome(s: string): boolean {
216226
}
217227
```
218228

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+
219239
### **Rust**
220240

221241
```rust

‎solution/0100-0199/0125.Valid Palindrome/README_EN.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ public class Solution {
139139
### **JavaScript**
140140

141141
```js
142-
const isPalindrome1 = function (s) {
142+
/**
143+
* @param {string} s
144+
* @return {boolean}
145+
*/
146+
var isPalindrome = function (s) {
143147
let arr1 = [],
144148
arr2 = [];
145149
for (let i = 0; i < s.length; i++) {
@@ -154,8 +158,14 @@ const isPalindrome1 = function (s) {
154158
arr2.reverse();
155159
return arr1.join('') === arr2.join('');
156160
};
161+
```
157162

158-
const isPalindrome = function (s) {
163+
```js
164+
/**
165+
* @param {string} s
166+
* @return {boolean}
167+
*/
168+
var isPalindrome = function (s) {
159169
function isNumOrAl(a) {
160170
if (
161171
(a >= 'A' && a <= 'Z') ||
@@ -215,6 +225,16 @@ function isPalindrome(s: string): boolean {
215225
}
216226
```
217227

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+
218238
### **Rust**
219239

220240
```rust
Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const isPalindrome1 = function (s) {
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (s) {
26
let arr1 = [],
37
arr2 = [];
48
for (let i = 0; i < s.length; i++) {
@@ -13,38 +17,3 @@ const isPalindrome1 = function (s) {
1317
arr2.reverse();
1418
return arr1.join('') === arr2.join('');
1519
};
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-
};

0 commit comments

Comments
 (0)
Please sign in to comment.