-
Notifications
You must be signed in to change notification settings - Fork 2
/
125.验证回文串.js
84 lines (78 loc) · 1.64 KB
/
125.验证回文串.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* @lc app=leetcode.cn id=125 lang=javascript
*
* [125] 验证回文串
*
* https://leetcode-cn.com/problems/valid-palindrome/description/
*
* algorithms
* Easy (41.06%)
* Likes: 161
* Dislikes: 0
* Total Accepted: 81K
* Total Submissions: 190.5K
* Testcase Example: '"A man, a plan, a canal: Panama"'
*
* 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
*
* 说明:本题中,我们将空字符串定义为有效的回文串。
*
* 示例 1:
*
* 输入: "A man, a plan, a canal: Panama"
* 输出: true
*
*
* 示例 2:
*
* 输入: "race a car"
* 输出: false
*
*
*/
// @lc code=start
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (input) {
var start = 0
var end = input.length - 1
while (start < end) {
var s = input.charCodeAt(start)
var e = input.charCodeAt(end)
if (!isLetter(s)) {
start++
continue
}
if (!isLetter(e)) {
end--
continue
}
if (toLowerCase(s) !== toLowerCase(e)) {
return false
}
start++
end--
}
return true
};
var isLetter = function(code) {
if (((code >= 48) && (code <= 57)) // numbers
|| ((code >= 65) && (code <= 90)) // uppercase
|| ((code >= 97) && (code <= 122))) { // lowercase
return true
}
else {
return false
}
}
var toLowerCase = function(code) {
if (code >= 65 && code <= 90) {
return code + 32
}
else {
return code
}
};
// @lc code=end