题目

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代码

  1. class Solution {
  2. public:
  3. bool isPalindrome(string s) {
  4. if (s.empty())
  5. return true;
  6. int size = s.length();
  7. int lhs = 0, rhs = size - 1;
  8. while (lhs < rhs)
  9. {
  10. if (!isalpha(s[lhs]))
  11. {
  12. ++lhs;
  13. continue;
  14. }
  15. if (!isalpha(s[rhs]))
  16. {
  17. --rhs;
  18. continue;
  19. }//if
  20. if (s[lhs] != s[rhs])
  21. return false;
  22. else
  23. {
  24. ++lhs;
  25. --rhs;
  26. }
  27. }//while
  28. return true;
  29. }
  30. //判断是否是字母数字,如果是大写字母则将其转化为小写字母
  31. bool isalpha(char &c){
  32. if ((c >= 'A'&&c <= 'Z')){
  33. c = c - 'A' + 'a';
  34. return true;
  35. }
  36. return (c >= 'a'&&c <= 'z') || (c >= '0'&&c <= '9');
  37. }
  38. };

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. UVA10129:Play on Words(欧拉回路)

    Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to s ...

  2. 洛谷 P1094 纪念品分组

    P1094 纪念品分组 先按价格对纪念品排序(这里是从大到小),然后从两端向中心开始配对,有两个变量i和j,表示正在处理的两个纪念品编号,开始时i=1,j=n,如果a[i]+a[j]>w则第i贵 ...

  3. UG 常用设置

    Q01:UG制图,添加基本视图之后的中心线怎么去掉? A01:“菜单-->文件-->首选项-->制图-->视图-->公共-->常规-->□带中心线创建”,取消 ...

  4. ajax中get和post区别

    参考地址:http://blog.csdn.net/laijieyao/article/details/40426257 首先要明确的事$.get方法是使用GET方式进行异步请求.$.post方法使用 ...

  5. odoo8 报表页面修改和字体设置

    版本8.0, 想要发票修改报表页眉的内容,去公司设置下修改,返现无论如何也不生效. 放狗后得知: You probably already know that you can customise th ...

  6. mysql join操作

    join的类型 1.  内联结:将两个表中存在联结关系的字段符合联结关系的那些记录形成记录集的联结. 2.  外联结:分为外左联结和外右联结. 案例背景 create table java (name ...

  7. Vue 简单实用---代码可以直接用

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  8. CSS子元素设置margin-top作用于父容器?

    CSS子元素设置margin-top作用于父容器? 原因: In this specification, the expression collapsing margins means that ad ...

  9. 爬虫的两种解析方式 xpath和bs4

    1.xpath解析 from lxml import etree 两种方式使用:将html文档变成一个对象,然后调用对象的方法去查找指定的节点 (1)本地文件 tree = etree.parse(文 ...

  10. npm install -g cnpm --registry=https://registry.npm.taobao.org

    npm install -g cnpm --registry=https://registry.npm.taobao.org