leetcode 125 验证回文字符串 Valid Palindrome

验证回文字符串

 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格;然后遍历第二遍,首尾一一对应比较;时间复杂度O(n+n/2),空间O(n);

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         string str;
 5         for(int i=0;i<s.size();i++){
 6             if( ('a'<=s[i]&&s[i]<='z')||('0'<=s[i]&&s[i]<='9') ){
 7                 str.push_back(s[i]);
 8             }else if('A'<=s[i]&&s[i]<='Z'){
 9                 str.push_back(tolower(s[i]));
10             }
11         }
12         int m=0,n=str.size()-1;
13         cout<<str;
14         while(m<n){
15             if(str[m]!=str[n]){
16                 return false;
17             }
18             m=m+1;n=n-1;
19         }
20         return true;
21     }
22 };

 

posted @ 2019-02-27 22:25  Joel_Wang  阅读(120)  评论(0编辑  收藏  举报