leetcode - Valid Palindrome
2013-12-10 15:44 张汉生 阅读(151) 评论(0) 编辑 收藏 举报
1 class Solution { 2 public: 3 int getNum(char c){ 4 if (c>='a' && c<='z') 5 return c-'a'; 6 if (c>='A' && c<= 'Z') 7 return c-'A'; 8 if (c>='0' && c<='9') 9 return 30 + (c-'0'); 10 return -1; 11 } 12 bool isPalindrome(string s) { 13 // IMPORTANT: Please reset any member data you declared, as 14 // the same Solution instance will be reused for each test case. 15 int len = s.length(); 16 int i =0, j = len-1; 17 while (i<j && i<len && j>=0){ 18 char x = s.at(i); 19 int a = getNum(x); 20 if (a==-1){ 21 i++; 22 continue; 23 } 24 char y = s.at(j); 25 int b = getNum(y); 26 if (b==-1){ 27 j--; 28 continue; 29 } 30 if (a!=b) 31 return false; 32 i++; 33 j--; 34 } 35 return true; 36 } 37 };