leetcode 125 验证回文串
2019-05-22 20:23
459 查看
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: “A man, a plan, a canal: Panama”
输出: true
示例 2:
输入: “race a car”
输出: false
python版本
class Solution: def isPalindrome(self, s: str) -> bool: s=''.join(re.split('\W+',s)).lower() return s==s[::-1]
C++版本
class Solution { public: bool isPalindrome(string s) { int i = next_alpha_numeric(s, 0); int j = prev_alpha_numeric(s, s.size() - 1); while(i <= j){ if(tolower(s[i]) != tolower(s[j])) return false; i = next_alpha_numeric(s, i + 1); j = prev_alpha_numeric(s, j - 1); } return true; } private: int next_alpha_numeric(const string& s, int index){ for(int i = index ; i < s.size() ; i ++) if(isalnum(s[i])) return i; return s.size(); } int prev_alpha_numeric(const string& s, int index){ for(int i = index ; i >= 0 ; i --) if(isalnum(s[i])) return i; return -1; } };
相关文章推荐
- [leetcode]验证回文串[javascript]
- LeetCode 125. 验证回文串
- leetcode-125. Valid Palindrome(验证含有标点符号String是否为回文串)
- Leetcode刷题28-125.验证回文串(C++)
- LeetCode 125.验证回文串
- 【LeetCode-面试算法经典-Java实现】【125-Valid Palindrome(回文字验证)】
- 125. 验证回文串 Java
- 验证回文串 - LeetCode
- Python LeetCode(125.验证回文串)
- [LeetCode 125] - 验证回文(Valid Palindrome)
- Leetcode题库 - 验证回文串(java语言版)
- Leetcode:125.验证回文串
- LeetCode-125.验证回文串(相关话题:字符串)
- LeetCode - 125. 验证回文串
- LeetCode 125 Valid Palindrome
- LeetCode(125) Valid Palindrome
- LeetCode125——Valid Palindrome
- Leetcode Shortest Palindrome (最短回文串)
- Python Leetcode(953.验证外星语词典)
- leetcode-409. Longest Palindrome 最长回文串的长度