博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Valid Palindrome
阅读量:4070 次
发布时间:2019-05-25

本文共 1119 字,大约阅读时间需要 3 分钟。

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.

问题:要进行比对的只有字母和数字,其他的标点等特殊字符跳过,问这些数字和字母的序列是否为回文。

class Solution {public:    bool alphanumeric (char c)    {        return (c >= 'a' && c <= 'z' || c >= '0' && c <= '9');    }    void tolower(string &s)    {        for(int i = 0; i < s.size(); ++i)        if(s[i] <= 'Z' && s[i] >= 'A')        s[i] += 32;    }    bool isPalindrome(string s) {        if(s.size() <= 1) return true;        int i = 0;        int j = s.size() - 1;        tolower(s);        while(i <= j)        {            if(alphanumeric(s[i]) && alphanumeric(s[j]) && s[i] == s[j])            {++i; --j;}            else if(!alphanumeric(s[i])) ++i;            else if(!alphanumeric(s[j])) --j;            else  return false;        }        return true;    }};

转载地址:http://gelji.baihongyu.com/

你可能感兴趣的文章
ceph - crushmap 扩容记录
查看>>
openstack 管理二十三 - nova compute 连接 ceph 集群
查看>>
openstack 管理二十四 - ceph 与 vm 连接测试记录
查看>>
openstack 管理二十五 - rpm 方式部署 openstack(架构说明)
查看>>
openstack 管理二十六 - rpm 方式部署 openstack [mariadb]
查看>>
openstack 管理二十八 - rpm 方式部署 openstack [keystone]
查看>>
openstack 管理二十九 - rpm 方式部署 openstack [glance]
查看>>
openstack 管理三十二 - rpm 方式部署 openstack [neutron]
查看>>
openstack 管理三十一 - rpm 方式部署 openstack [nova]
查看>>
openstack 管理三十三 - rpm 方式部署 openstack [compute]
查看>>
openstack 管理三十四 - neutron dhcp agent 管理
查看>>
logstash + grok 正则语法
查看>>
bandwitdthd 监控
查看>>
rrdtool-1.4.5 compile in rhel6
查看>>
nginx 技巧
查看>>
oracle omf
查看>>
自定义 mrtg 数据
查看>>
rsync , rsync + ssh, rsync + lsyncd 多种同步方案与比较
查看>>
rhel6 网卡定义注意事项
查看>>
ceph 数据恢复检测
查看>>