博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Valid Palindrome
阅读量:7135 次
发布时间:2019-06-28

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

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.

 

Hide Tags
   
 

 注意 extern int toupper(int c); 如果c为小写英文字母,则返回对应的大写字母;否则返回原来的值。

class Solution {    public:        bool isPalindrome(string s) {            if(s.empty())                return true;            int idx1 = 0;            int idx2 = s.size()-1;            while(idx1 < idx2)            {                while(!isalnum(s[idx1]) && idx1 < idx2)                {                    idx1 ++;                }                while(!isalnum(s[idx2]) && idx1 < idx2)                {                    idx2 --;                }                if(idx1 >= idx2)                    break;                if(toupper(s[idx1]) == toupper(s[idx2]))                {                    idx1 ++;                    idx2 --;                }                else                    return false;            }            return true;        }};

 

使用transform 来更改字母的大小写

class Solution {    public:        bool isPalindrome(string s) {            transform(s.begin(), s.end(), s.begin(), ::tolower);            auto left = s.begin(), right = prev(s.end());            while (left < right) {                if (!::isalnum(*left)) ++left;                else if (!::isalnum(*right)) --right;                else if (*left != *right) return false;            }            return true;        }};

 

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

你可能感兴趣的文章
实战:通过建立的会话查看***
查看>>
Ionic CLI 升级到最新版本
查看>>
移动大时代:烽火Exmobi的主打歌
查看>>
java 递归
查看>>
Linux Shell脚本测试案例(二)
查看>>
常用节点类型
查看>>
2.4-hash类型常用命令
查看>>
MongoDB 用户认证
查看>>
一些开源镜像站
查看>>
RAID
查看>>
linux rpm安装webmin
查看>>
DDOS***原理与防护
查看>>
懒加载 与 富文本编辑器 的 相爱相杀
查看>>
我的友情链接
查看>>
java转换是json需要的jar包导致的问题,
查看>>
mac安装brew和nginx
查看>>
技术博客
查看>>
软考信息系统监理师:2016年4月1日作业
查看>>
关于ssh远程登录太慢的解决方法
查看>>
bootstrap栅格系统显示规则
查看>>