[LeetCode] Valid Palindrome
时间:2014-03-14 17:04:14
收藏:0
阅读:382
Given 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.
Solution:
class Solution { public: bool isPalindrome(string s) { string ss = ""; for(int i = 0;i < s.length();i++) { if(s[i] >= ‘A‘ && s[i] <= ‘Z‘) ss += s[i] - ‘A‘ + ‘a‘; else if(s[i] >= ‘a‘ && s[i] <= ‘z‘) ss += s[i]; else if(s[i] >= ‘0‘ && s[i] <= ‘9‘) ss += s[i]; } if(ss.length() < 2) return true; else { for(int i = 0;i < ss.length() / 2;i++) { if(ss[i] != ss[ss.length() - 1 - i]) return false; } return true; } } };
[LeetCode] Valid Palindrome,布布扣,bubuko.com
原文:http://www.cnblogs.com/changchengxiao/p/3600322.html
评论(0)