题目

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.

分析

判断给定字符串是否为忽略大小写,特殊符号,空格等之后的回文串。

AC代码

class Solution {
public:
bool isPalindrome(string s) {
if (s.empty())
return true; int size = s.length(); int lhs = 0, rhs = size - 1;
while (lhs < rhs)
{
if (!isalpha(s[lhs]))
{
++lhs;
continue;
}
if (!isalpha(s[rhs]))
{
--rhs;
continue;
}//if if (s[lhs] != s[rhs])
return false;
else
{
++lhs;
--rhs;
} }//while
return true;
}
//判断是否是字母数字,如果是大写字母则将其转化为小写字母
bool isalpha(char &c){
if ((c >= 'A'&&c <= 'Z')){
c = c - 'A' + 'a';
return true;
}
return (c >= 'a'&&c <= 'z') || (c >= '0'&&c <= '9');
}
};

GitHub测试程序源码

LeetCode(125) Valid Palindrome的更多相关文章

  1. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  2. LeetCode(242)Valid Anagram

    题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...

  3. LeetCode(49)-Valid Parentheses

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  4. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  5. LeetCode(20)Valid Parentheses

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  6. LeetCode(125):验证回文串

    Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...

  7. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  8. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. 复习线程——状态和几个Thread方法

    一.线程的状态 (参考文章:https://blog.csdn.net/a58220655/article/details/76695142) 状态介绍 新建(new):处于该状态的时间很短暂.已被分 ...

  2. [译]Understanding ECMAScript6 迭代器与生成器(未完)

    迭代器在许多编程语言中被作为一种更易处理数据集合的方式被使用.在ECMAScript6中,JavaScript添加了迭代器,将其作为此语言的一个重要特征.当再加上新的方法和新的集合类型(比如集合与映射 ...

  3. attribute与property区别总结

    在前阵子看JQuery源码中,attr()的简单理解是调用了element.getAttribute()和element.setAttribute()方法,removeAttr()简单而言是调用ele ...

  4. mac Latex dvipdfm 缺少字体错误 Failed to read UCS2/UCS4 TrueType cmap

    dvipdfmx 命令产生 ** ERROR ** Failed to read UCS2/UCS4 TrueType cmap... 错误的原因是没有把 simsun.ttf simkai.ttf ...

  5. 后端 node 项目工具集

    后端 node 项目工具集 editor vs code webstorm 质量检查 eslint prettier 命令行相关 better-run-npm npm-run-all nodemon ...

  6. 移动端REM布局模板(阿里高清方案)

    移动端REM布局模板(阿里高清方案),蛮好的,转自: http://www.jianshu.com/p/985d26b40199 . <!DOCTYPE html> <html la ...

  7. 关于nodejs模块安装后找不到包解决办法

    主要原因是类似bower.gulp这些包后,没有添加到环境变量,但是有洁癖的我也不希望添加太多的软链接,所以在用phpstorm开始时有需要的情况下 定义临时的环境变量 http://stackove ...

  8. 继承UIView的初始化 、重绘、以及绘制图片

    大家对于UIViewController的生命周期都相当了解了.但是对于继承UIView的子类能做什么,却很少有文章介绍的. 1.  -initWithFrame:(CGRect)rect是view指 ...

  9. shell中的-z

    -z 字符串为"null",即是指字符串长度为零.

  10. 洛谷 P1181 数列分段Section I(水题日常)

    题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求. 输入输出格式 输入格式: 输入文件divide_ ...