Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a255ba7

Browse files
committed
docs: 新增No.125 题解
1 parent cb321d1 commit a255ba7

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎leetcode刷题/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [No.48 旋转图像](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No48_rotate.md)
1010
- [No.66 加一](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No66_plus-one.md)
1111
- [No.122 买卖股票的最佳时机 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No122_max-profit.md)
12+
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
1213
- [No.136 只出现一次的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No136_single-number.md)
1314
- [No.141 环形链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No141_has_cycle.md)
1415
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
@@ -45,6 +46,7 @@
4546
#### 字符串初级
4647

4748
- [No.7 整数反转](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No7_reverse.md)
49+
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
4850
- [No.242 有效的字母异位词](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No242_is-anagram.md)
4951
- [No.387 字符串中的第一个唯一字符](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No387_first-uniq-char.md)
5052

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# No.125 验证回文串
2+
3+
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
4+
5+
说明:本题中,我们将空字符串定义为有效的回文串。
6+
7+
## 示例
8+
9+
示例 1:
10+
11+
输入: "A man, a plan, a canal: Panama"
12+
输出: true
13+
示例 2:
14+
15+
输入: "race a car"
16+
输出: false
17+
18+
## 解题思路
19+
20+
有题目条件可知,字符串中含有其他字符,所以可以先将非字母、数字的数组去除,再通过数组判断是否为回文字符串。
21+
22+
```javascript
23+
/**
24+
* @param {string} s
25+
* @return {boolean}
26+
*/
27+
var isPalindrome = function(s) {
28+
let arr = [];
29+
for (let char of s) {
30+
let charCode = char.charCodeAt();
31+
if (('0'.charCodeAt()<=charCode && charCode<='9'.charCodeAt())
32+
|| ('a'.charCodeAt()<=charCode && charCode<='z'.charCodeAt())
33+
|| ('A'.charCodeAt()<=charCode && charCode<='Z'.charCodeAt()) ) {
34+
35+
arr.push(char.toLowerCase());
36+
37+
}
38+
}
39+
40+
for (let i = 0; i < arr.length/2; i++) {
41+
if (arr[i] !== arr[arr.length-1-i]) {
42+
return false;
43+
}
44+
}
45+
return true;
46+
};
47+
```

‎leetcode刷题/note/No2_is-palindrome.md‎

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /