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. JAVA FILE.renameTo跨文件系统移动文件失败

    遇到了FILE.renameTo跨文件系统移动文件失败的问题,应使用FILES.move()接口或在同一文件系统移动文件. FILE.renameTo接口说明: public boolean rena ...

  2. Jmeter登录接口返回 status415

    1.现象:在查看结果树中看到: Request Headers:Connection: keep-aliveContent-Type: application/x-www-form-urlencode ...

  3. 【JSON类】使用说明

    理解键名路径 键名路径(keyPath)用于定位json的键,比如:{book: {title:”中国人”} },键名路径 book.title 表定位到book下的title键. 对于值是数组类型的 ...

  4. 垃圾收集器与内存分配策略(深入理解Java虚拟机)

    3.1 概述 垃圾收集器要解决哪些问题? 哪些内存需要回收 什么时候回收 如何回收 引用计数算法:当有一个地方引用,+1,引用失效,-1.     缺点:对象之间相互循环引用的问题. 可达性分析算法: ...

  5. 骰子涂色 (Cube painting,UVa 253)

    题目描述:算法竞赛入门习题4-4  题目思路:1.旋转其中一个骰子进行匹配 2.进行遍历,如果匹配,就进行相对面的匹配 3.三个对立面都匹配即是一样等价的 //没有按照原题的输入输出 #include ...

  6. SVG Sprite 使用Symbol元素制作ICON

    介绍 SVG是一种全新的使用方式,应该说这才是未来的主流,也是平台目前推荐的用法.之前写过两篇关于CSS icon在页面显示的博客,后来了解到现在大多数前端团队和项目都在使用SVG Sprite这种方 ...

  7. struts2 result type属性说明

    首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-type name="chain" ...

  8. 十二:NodeManager

    NM负责启动和管理节点上的containers.AM通过containers来运行任务. Health Checker Service 创建检查服务     NM运行一个检查服务来检查节点的状态,该服 ...

  9. 一些容易记混的c++相关知识点

    一些容易记混的c++相关知识. 截图自:<王道程序员面试宝典>

  10. [leetcode-658-Find K Closest Elements]

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...