【题目】

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.

【题意】

给定一个字符串,仅仅关注英文字母和数字,忽略其它字符。问英文字母和数字按序排列后是否构成是回文

【思路】

利用回文串推断的老办法就可以,两个指针p1, p2分别从头和尾中间扫描,推断对称位置的字符是否同样,仅仅只是须要跳过除英文字母和数字之外的其它字符。

    另外还须要注意处理2中特殊情况:(1)字符串为空串,(2)字符串不是空串,但里面没有英文字母或者数字。 这两种情况都判定为true

【代码】

  1. class Solution {
  2. public:
  3. bool isAlphanumeric(char c){
  4. if(isdigit(c))return true;
  5. if(c>='A'&&c<='Z'||c>='a'&&c<='z')return true;
  6. return false;
  7. }
  8.  
  9. bool isEqual(char c, char b){
  10. if(isdigit(c))return c==b;
  11. if(c>='A'&&c<='Z')c='a'+(c-'A');
  12. if(b>='A'&&b<='Z')b='a'+(b-'A');
  13. return c==b;
  14. }
  15.  
  16. bool isPalindrome(string s) {
  17. int len=s.length();
  18. if(len==0)return true;
  19.  
  20. int front=0;
  21. int back=len-1;
  22. while(front<back){
  23. while(front<=back && !isAlphanumeric(s[front]))front++;
  24. while(front<=back && !isAlphanumeric(s[back]))back--;
  25. if(front<=back){
  26. if(!isEqual(s[front], s[back]))return false;
  27. front++;
  28. back--;
  29. }
  30. }
  31. return true;
  32. }
  33. };

LeetCode: Valid Palindrome [125]的更多相关文章

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

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

  2. LeetCode——Valid Palindrome

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

  3. [LeetCode] Valid Palindrome II 验证回文字符串之二

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  4. [leetcode]Valid Palindrome @ Python

    原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...

  5. LeetCode Valid Palindrome II

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/ 题目: Given a non-empty string  ...

  6. Leetcode Valid Palindrome

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

  7. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  8. [Leetcode] valid palindrome 验证回文

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

  9. leetcode Valid Palindrome C++&amp;python 题解

    题目描写叙述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

随机推荐

  1. SilkTest Q&A 10

    92. 如何把单个表达式分两行来写? 答案1: 使用Shift + Enter 答案2: 很容易在online help里面找到答案: 1)      line break in code 2)    ...

  2. jsp:setProperty

    类声明: package test; public class Student {     private int age; public int getAge() {         return ...

  3. dialog开发

    dialog开发屏幕编程:ok_code在程序里用sy-ucomm接受 调用其他事物代码:call transaction ‘SE38’. 1:50 选择屏幕之屏幕按钮: selection-scre ...

  4. Android菜鸟的成长笔记(12)——Handler、Loop、MessageQueue

    原文:[置顶] Android菜鸟的成长笔记(12)——Handler.Loop.MessageQueue 当一个程序第一次启动时,Android会启动一条主线程(Main Thread),主线程主要 ...

  5. 联想A800新蜂ROM V1.1 基于官方4.0.4精简省电稳定

    ROM介绍 [出品]:新蜂工作室(基于官方) 1.源于官方:基于最稳定官方底包制作. 2.深度精简:自带APK数量从原厂包的131个降低到90个,精简31% 3.ROM包大小从原厂314MB精简到16 ...

  6. Swift - 选择框(UIPickerView)的用法

    1,选择框可以让用户以滑动的方式选择值.示例如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  7. SQL--存储过程+触发器 对比!

    一.存储过程 一:存储过程:存储过程是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中. 可以用存储过程名字和参数来调用存储过程,这样可以避免代码重复出现,用起来也方便. 例:    下面 ...

  8. C++的for循环细节,必看!

    C++中.For(A;B;C)   C语句是在每次循环后才运行. 如: y=10; for( i=0;i<10;y=++i) {    cout<<y<<endl; } ...

  9. 《Head First 设计模式》学习笔记——模板方法模式

    模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以详细方法以及详细构造函数的形式实现.然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类能够以不同的方式实现这些抽象方法,从而对剩余的逻辑有 ...

  10. apk应用的反编译和源代码的生成

    对于反编译一直持有无所谓有或无的态度.经过昨天一下午的尝试,也有了点心得和体会: 先给大家看看编译的过程和我们反编译的过程概图吧: 例如以下是反编译工具的根文件夹结构: 三个目录也实际上是下面三个步骤 ...