去年就在某牛人的博客上见到了,现在准备去玩玩。做题的方式跟Topcoder类似,刚刚去水了一题"Valid Palindrome"
1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 string alnumstr; 7 transform(s.begin(), s.end(), s.begin(), [](char& ch){ return ::toupper(ch);}); 8 copy_if(s.begin(), s.end(), back_inserter(alnumstr), [](char& ch){ return ::isalnum(ch);}); 9 10 string another;11 reverse_copy(alnumstr.begin(), alnumstr.end(), back_inserter(another));12 13 return another == alnumstr;14 15 }16 };
1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 7 const char* str = s.c_str(); 8 for (int i = 0, j = strlen(str); i < j; ) 9 {10 if (isalnum(str[i]) && isalnum(str[j]))11 {12 if (toupper(str[i]) != toupper(str[j]))13 {14 return false;15 }16 else17 {18 ++i;19 --j;20 }21 }22 if (!isalnum(str[i])) ++i;23 if (!isalnum(str[j])) --j;24 }25 return true;26 27 }28 };