Leetcode Valid Palindrome
来源:互联网 发布:新闻娱乐化 知乎 编辑:程序博客网 时间:2024/06/05 03:04
Valid Palindrome
Total Accepted: 6669 Total Submissions: 31102My SubmissionsGiven a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
很简单的题目: 2星级。
考点:
1 会判断是否是字母和数字
2 知道什么是Palindrome
可以利用函数库判断是否为数字或者字母,程序如下:
bool isPalindrome(string s) {for (int i = 0, j = s.size()-1; i < j; i++, j--){while (i<j && (!isalpha(s[i]) && !isdigit(s[i]))) i++;while (i<j && (!isalpha(s[j]) && !isdigit(s[j]))) j--;if (i<j && tolower(s[i]) != tolower(s[j])) return false;}return true;}
自家写函数判断也很简单:
bool isPalindrome(string s) {for (int i = 0, j = s.size()-1; i < j; i++, j--){while (s[i] && (!isTestChar(s[i]))) i++;while (s[j] && (!isTestChar(s[j]))) j--;if (i<j && !isEqual(s[i], s[j])) return false;}return true;}bool isTestChar(char a){int i = a;return (i>=97 && i<=122) || (i>=65 && i<=90) || (i>=48 && i<=57);}bool isEqual(char a, char b){return a == b || int(a) == int(b)-32 || int(b) == int(a)-32;}/*char a = 'A';//65a = 'a';//97a = 'Z';//90a = 'z';//122a = '1';//49a = '0';//48*/
//2014-2-18 updatebool isPalindrome(string s) {for (int i = 0, j = s.length()-1; i < j; ){if (isalnum(s[i]) && isalnum(s[j])){if (tolower(s[i]) != tolower(s[j])) return false;i++, j--;}if (i<j && !isalnum(s[i])) i++;if (i<j && !isalnum(s[j])) j--;}return true;}
1 1
- [Leetcode] Valid Palindrome
- Leetcode: Valid Palindrome
- leetcode 55: Valid Palindrome
- [LeetCode] Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode - Valid Palindrome
- [leetcode]Valid Palindrome
- [LeetCode]Valid Palindrome
- [Leetcode]Valid Palindrome
- LeetCode-Valid Palindrome
- [leetcode] Valid Palindrome
- LeetCode 125: Valid Palindrome
- LeetCode - Valid Palindrome
- leetcode之Valid Palindrome
- LeetCode | Valid Palindrome
- [leetcode]Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode题解:Valid Palindrome
- 关于java.lang.OutOfMemory的分类
- WIX 资料参考
- QLineEdit、QLabel字体大小、颜色设置
- 时间在变,人也在变。
- JTBC-ASP版本首页标签调用说明
- Leetcode Valid Palindrome
- display:block
- 数字签名和数字证书
- Oc_总结
- blog
- JTBC-ASP版本列表页标签调用说明
- google perftools分析程序性能
- Visual C++ ADO数据库编程入门(上)
- Visual C++ ADO数据库编程入门(下)