题目

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. 解决windows下 Python中 matplotlib 做图中文不显示的问题

    在代码中填入以下两句即可 from pylab import mpl mpl.rcParams['font.sans-serif'] = [font_name] 如:mpl.rcParams['fon ...

  2. css水平垂直居中块整理

    1.绝对定位+负margin 兼容性很好,但需要指定子块的高度和宽度,以及负margin .wp{ position: relative; width: 200px; height: 200px; b ...

  3. Oracle 修改密码(忘记登录密码,用户System)

    1.修改计算机环境变量,把oracle服务端路径放在最前面 2.输入cmd 3.输入命令:sysplus /nolog SQL>conn sys/syspwd as sysdba SQL> ...

  4. P1202 [USACO1.1]黑色星期五Friday the Thirteenth

    题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...

  5. Java——HashSet和TreeSet的区别

    HashSetHashSet有以下特点 不能保证元素的排列顺序,顺序有可能发生变化 不是同步的 集合元素可以是null,但只能放入一个null当向HashSet集合中存入一个元素时,HashSe ...

  6. CF739B

    深搜的过程中保存路径,二分路径中满足要求的区段.不必将每个节点的ans加1,只需将合法区段末尾加1同时将开头减1来表示并保存在一个“前缀”数组中即可.最后再dfs一次累加得到答案. #include ...

  7. iOS之创建CocoaPods公有库教程

    简介 在开发过程中,经常会使用到第三框架,我们通过一个pod install命令,很方便的就将第三方框架加到我们自己的项目中. 如果我们也想将自己写的组件或库开源出去,让别人也可以通过pod inst ...

  8. Eclipse下对MAVEN进行junit软件测试

    一.Maven project management and build automation tool, more and more developers use it to manage the ...

  9. HDU 4276 The Ghost Blows Light (树形DP,变形)

    题意:给定一棵n个节点的树,起点是1,终点是n,每经过一条边需要消耗Ti天,每个点上有一定量的珠宝,要求必须在t天内到达终点才可以将珠宝带出去,问至多能带多少珠宝? 思路: 注意Ti可以为0,而且有可 ...

  10. HDU 1729 Stone Game 石头游戏 (Nim, sg函数)

    题意: 有n个盒子,每个盒子可以放一定量的石头,盒子中可能已经有了部分石头.假设石头无限,每次可以往任意一个盒子中放石头,可以加的数量不得超过该盒中已有石头数量的平方k^2,即至少放1个,至多放k^2 ...