注意:
1.ctype.h这个库里有toupper()、isdigit()、isalpha()、这些函数。这样就不用自己写了,虽然自己写也简单,但是记住这些功能就会方便很多。
做了这道题,我还看了ctype库里还有判断是否为空白,空格,十六进制,操作符,标点符号等函数,记住他的这些功能然后以后用到的时候就可以去搜。
我的C++代码(多写了自己的判断代码):
class Solution {
public:
/**
* @param s A string
* @return Whether the string is a valid palindrome
*/
bool isPalindrome(string& s) {
if (s=="")
return true;
string temp = "";
for(int i=0; i<s.size(); i++){
if (isalpha(s[i])) {
temp += toupper(s[i]);
} else if (isdigit(s[i])) {
temp += s[i];
}
}
int i=0, j=temp.size()-1;
while(i<j) {
if (temp[i] != temp[j])
return false;
i++;
j--;
}
return true;
}
inline bool isChar(char & a) {
if ((a>='A'&&a<='Z')||(a>='a'&&a<='z'))
return true;
return false;
}
inline bool isNum(char & a) {
if (a>='0' && a<='9')
return true;
return false;
}
};