From fd2dbd07bd57e9ee76b8dae868e348f843bcacfc Mon Sep 17 00:00:00 2001 From: xliu45 Date: Thu, 10 Mar 2016 21:16:21 -0600 Subject: [PATCH] Add 125_Valid_Palindrome.cpp --- C++/125_Valid_Palindrome.cpp | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 C++/125_Valid_Palindrome.cpp diff --git a/C++/125_Valid_Palindrome.cpp b/C++/125_Valid_Palindrome.cpp new file mode 100644 index 00000000..52220f4e --- /dev/null +++ b/C++/125_Valid_Palindrome.cpp @@ -0,0 +1,38 @@ +//125. Valid Palindrome +/*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. + +Author: Xinyu Liu +*/ + +#include +using namespace std; + +class Solution { +public: + bool isPalindrome(string s) { + string::iterator start = s.begin(); + string::iterator end = s.end() - 1; + while(start < end){ + while (start < end && !isalnum(*start)) + start ++; + while (start < end && !isalnum(*end)) + end --; + if (tolower(*start )!= tolower(*end)) + return false; + start ++; + end--; + } + return true; + + } +}; + +void main(){ + string str_test = "test"; + Solution sol; + bool bol = sol.isPalindrome(str_test); +}