Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

125. 验证回文串 #33

Closed
sailei1 opened this issue May 21, 2019 · 0 comments
Closed

125. 验证回文串 #33

sailei1 opened this issue May 21, 2019 · 0 comments

Comments

@sailei1
Copy link
Owner

sailei1 commented May 21, 2019

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:

输入: "race a car"
输出: false

解法
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。
先过滤 再 反转后 跟原字符对比

/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(s) {
    let str=s.toLowerCase().replace(/[^a-z0-9]/g,''); //过滤
    let re=str.split('').reverse().join(''); // 反转
   return str===re
};
@sailei1 sailei1 closed this as completed May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant