[C++]LeetCode: 25 Valid Palindrome

来源:互联网 发布:access2007数据库实例 编辑:程序博客网 时间:2024/05/11 01:59

题目:

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.

判断一个字符串是否是回文,只考虑字母和数字,忽略符号。定义空字符为空回文。

ORI_思路

思路:得到翻转的字符串,然后比较是否相同,将符号和空格去掉,都转为小写,得到S,ReversS.比较。

考虑几个问题:1.空回文 2.去掉符号和空格 3.strcmp比较ASCII,所以要统一大小写。ASCII码48~57 数字; 65~90 大写字母; 97~122 小写字母。

分析:1. 需要存储两个字符串,一个栈,效率低,占用太多内存。 2. 判断是否是字符数字,不需要具体ASCII,用对应字符就可以。

           3. 无法比较两个non const string.

Submission Result: Compile Error

Line 40: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’

Error Code:

class Solution {public:    bool isPalindrome(string s) {        //判断一个字符串是否是回文,只考虑字母和数字,忽略符号。        //定义空字符为空回文。        //思路:得到翻转的字符串,然后比较是否相同,将符号和空格去掉,都转为小写,得到S,ReversS.比较        //考虑几个问题:1.空回文 2.去掉符号和空格 3.strcmp比较ASCII,所以要统一大小写。        //ASCII码48~57 数字; 65~90 大写字母; 97~122 小写字母。                bool ret = false;        string lowerS;    //小写s         string revslowerS;  //翻转小写s        stack<char> strstack;         int strlen = s.length();                if(strlen == 0)            return true;                for(int i = 0 ; i < strlen; i++)        {            if((s[i] >= 65 && s[i] <= 90))            {                lowerS.push_back(s[i] + 32);                strstack.push(s[i] + 32);            }            else if((s[i] >= 48 && s[i] <= 57)|| (s[i] >=97 && s[i] <= 122))            {                lowerS.push_back(s[i]);                strstack.push(s[i]);            }        }                //翻转s        while(!strstack.empty())        {            revslowerS.push_back(strstack.top());            strstack.pop();        }                if(strcmp(lowerS, revslowerS) == 0)            ret = true;                    return ret;            }};

正解:

思路: 构造函数判断每个字符是否是数字字符,从正反两个方向遍历字符串,比较是否相等(i < j 循环)。不断往字符串中间逼近。

复杂度:O(N)

Attention: 需要把判断函数写在主函数外; 判断函数参数用引用类型, 直接进行大小写转换!!

AC Code:

class Solution {public:    //判断是否是数字或字符    bool isStr(char &ch){        if(ch >= '0' && ch <= '9'){            return true;        }        else if(ch >= 'a' && ch <= 'z'){            return true;        }        else if(ch >= 'A' && ch <= 'Z'){            ch += 32;            return true;        }        else{            return false;        }    }        bool isPalindrome(string s) {        int len = s.length();        if(len == 0)            return true;                int i = 0, j = len - 1;        while(i < j){            if(!isStr(s[i])){                ++i;            }            else if(!isStr(s[j])){                --j;            }            else if(s[i++] != s[j--]){                return false;            }        }                return true;    }};





0 0
原创粉丝点击