-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125.验证回文串.ts
More file actions
38 lines (34 loc) · 751 Bytes
/
Copy path125.验证回文串.ts
File metadata and controls
38 lines (34 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* @lc app=leetcode.cn id=125 lang=typescript
*
* [125] 验证回文串
*/
// @lc code=start
const isNumOrChar = (charOrNum) => {
if (charOrNum.charCodeAt(0) >= 'a'.charCodeAt(0) && charOrNum.charCodeAt(0) <= 'z'.charCodeAt(0) || (charOrNum !== ' ' && !isNaN(Number(charOrNum)))) {
return true
}
return false
}
function isPalindrome(s: string): boolean {
const lowerS = s.toLowerCase()
for (let i = 0, j = s.length - 1; i < j;) {
const lowerI = lowerS[i]
const lowerJ = lowerS[j]
if (!isNumOrChar(lowerI)) {
i++
continue
}
if (!isNumOrChar(lowerJ)) {
j--
continue
}
if (lowerS[i] !== lowerS[j]) {
return false
}
i++
j--
}
return true
};
// @lc code=end