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.

//用两个指针实现。前提是要忽略其它符号

class Solution {
public:
bool isPalindrome(string s) {
if(s.empty())
return true;
int i=0;
int j=s.size()-1;
for(;i<j;i++)
{//i后移
if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9'))
{//找到第一个为字符或数字的字符
for(;j>i;--j)
{//j前移
if((s[j]>='a' && s[j]<='z') || (s[j]>='A' && s[j]<='Z') || (s[j]>='0' && s[j]<='9'))
{//找到第一个为字符或数字的字符
if(s[i]==s[j] || s[i]+'A'-'a'==s[j] || s[i]+'a'-'A'==s[j])
{//找到后时行推断
j--;//前进一步
break;
}
else
return false;
}
}
}
}
return true;
}
};

leetCode(51):Valid Palindrome的更多相关文章

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

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

  2. [Leetcode][JAVA] Valid Palindrome

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

  3. [leetcode] 1. Valid Palindrome

    leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...

  4. LeetCode 1216. Valid Palindrome III

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...

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

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

  6. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  7. 【题解】【字符串】【Leetcode】Valid Palindrome

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

  8. leetcode 125. Valid Palindrome ----- java

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

  9. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

随机推荐

  1. BZOJ 4821 [Sdoi2017]相关分析 ——线段树

    打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...

  2. 从输入url到页面呈现的过程

    从输入url到页面呈现的过程包括两个基本过程:网络通信和页面渲染 网络通信主要过程是 域名解析 -> TCP连接 -> HTTP请求 -> 服务端响应,返回HTML 页面渲染的主要过 ...

  3. 算法复习——1D/1Ddp优化

    搬讲义~~~~ 题目1:玩具装箱(bzoj1010) Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一 ...

  4. kali2 install Nessus

    注册: https://www.tenable.com/products/nessus-home 安装: 设置登录用户名,密码,输入注册码:

  5. Java基础加强-(注解,动态代理,类加载器,servlet3.0新特性)

    1.   Annotation注解 1.1.  Annotation概述 Annotation是JDK 5.0以后提供对元数据的支持,可以在编译.加载和运行时被读取,并执行相应的处理.所谓Annota ...

  6. 转 php simple test

    转自 后期移至 以下为汪大哥写的 yunlian服务监控 如何写监控代码 首先在tests目录下新建一个文件xxx.php.其中xxx为你的服务名. class XxxTestCase extends ...

  7. Scanner用法

    先来看一个简单的例子: import java.util.*; public class ScannerTest { public static void main(String[] args){   ...

  8. Day 19 函数之闭包、装饰器

    一.什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 二.装饰器遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰 ...

  9. WEB学习-CSS行高、字体,链接的美化以及背景

    行高和字号 CSS中,所有的行,都有行高.盒模型的padding,绝对不是直接作用在文字上的,而是作用在“行”上的. 单行文本垂直居中 文本在行里面是居中 其中,行高:盒子高; 需要注意的是,这个小技 ...

  10. Reveal.js演讲幻灯片框架

    摘要 还需学习参考的链接  https://www.tuicool.com/articles/2AFFj2j 无意中看到这个插件,很喜欢,可以作用在演讲ppt,幻灯片,用户指引上等.代码简单,易维护 ...