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.

题意:给定字符串,判断是否为回文。值得注意的是,只考虑字符和数字且不考虑字符大写小,其他情况忽略。

思路:判断是否为回文的情况,一般都是用两个指针,从字符串的两端开始向中间移动,若对应位置的字符不等则返回false。这里的区别在于,有空格和符号,而这些都是题中要求忽略的,所以,每当遇到空格和符号时,直接跳过就行,反应在代码上就是一个加加,一个减减;还有一点就是,这里是不考虑大小写的,所以需要写一个函数,遇到大写的时候转化成小写去比较(也可以小转大),其他不变。代码如下:

 class Solution {
public:
bool isPalindrome(string s)
{
int len=s.size();
if(len==) return true; int l=,r=len-;
while(l<r)
{
if( !isAlpnum(s[l]))
l++;
else if( !isAlpnum(s[r]))
r--;
else if(toLower(s[l]) !=toLower(s[r]))
return false;
else
{
l++;
r--;
}
}
return true;
} bool isAlpnum(const char c)
{
if('A'<=c&&c<='Z')
return true;
else if('a'<=c&&c<='z')
return true;
else if(''<=c&&c<='')
return true;
return false;
} char toLower(const char c)
{
if('A'<=c&&c<='Z')
return c+;
return c;
}
};

其中判断是否为字母和数字可以用函数isalnum,这样就可以少写一个函数,其余不变。参考了booirror的博客。

[Leetcode] valid palindrome 验证回文的更多相关文章

  1. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  4. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. 125 Valid Palindrome 验证回文字符串

    给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...

  6. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  7. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. LeetCode 125. Valid Palindorme (验证回文字符串)

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

随机推荐

  1. Unity Lighting - The Precompute Process 预计算过程(二)

      The Precompute Process 预计算过程 In Unity, precomputed lighting is calculated in the background - eith ...

  2. Spring Cloud(十):服务网关 Zuul(路由)【Finchley 版】

    Spring Cloud(十):服务网关 Zuul(路由)[Finchley 版]  发表于 2018-04-23 |  更新于 2018-05-09 |  通过之前几篇 Spring Cloud 中 ...

  3. ubuntu ssh配置

    Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over ...

  4. nordic mesh中的消息缓存实现

    nordic mesh中的消息缓存实现 代码文件msg_cache.h.msg_cache.c. 接口定义 头文件中定义了四个接口,供mesh协议栈调用,四个接口如下所示,接口的实现代码在msg_ca ...

  5. 自测之Lesson15:TCP&UDP网络编程

    题目:编写一个TCP通信的程序. 实现代码: #include <stdio.h> #include <sys/socket.h> #include <unistd.h& ...

  6. 20145214 《Java程序设计》第4周学习总结

    20145214 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承基本上就是避免多个类间重复定义共同行为.要避免在程序设计上出现重复,可以把相同的程序代码提升为父类. 关键 ...

  7. c# windows service 程序

    service服务程序:可以长时间运行可执行应用程序.没有用户界面.可以自动启动和手动启动.适用于在服务器上或需要干扰其他工作的用户可以在同一台计算机上长时间的运行此功能. C#创建service服务 ...

  8. win10 死机

    其实Win10系统还是不错的,如果你的电脑升级Win10后中招经常死机,可以用下面的方案来应对. 1.下载安装支持兼容Win10的软件版本,下载软件之前看一下兼容列表里是否有Win10系统.虽然Win ...

  9. ACM 第十六天

    计算几何 练习题: F - Beauty Contest POJ - 2187 Bessie, Farmer John's prize cow, has just won first place in ...

  10. Debian 7 amd64 + fbterm + ucimf

    前段时间,显示器出了问题,导致Debian下只有终端显示正常,桌面显示效果很是摇晃模糊.遂起了念头,能不能在终端下就能完成日常的工作. google了很久,终于知道fbterm可以在终端下显示中文,加 ...