有效回文串

注意:

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;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值