一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:判断一个字符串是不是有效的回文字符串。忽略里面除了数字和字母的其他字符。

解题思路:两个指针i和j,i从前往后,j从后往前,碰到p[i]和p[j]都是字母或者数字就比较大小,如果相等就i++,j–反之则返回false

直到i>=j时,说明是有效回文,返回true。

//isalnum()是判断该字符是不是字母或数字
class Solution {
public:
    bool isPalindrome(string s) {
        int len = s.length();
        int i = 0;
        int j = len-1;
        while(i<=j)
        {
            while(!isalnum(s[i])&&i<=j) i++;//直到s[i]为字母或数字为止
            while(!isalnum(s[j])&&i<=j) j--;//直到s[j]为字母或数字为止
            if(i<=j&&tolower(s[i])!=tolower(s[j])) return false;//如果不等就返回false
            i++;j--;//反之就继续比较
        }
        return true;
    }
};

【一天一道LeetCode】#125. Valid Palindrome的更多相关文章

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

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

  2. leetcode 125. Valid Palindrome ----- java

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

  3. LeetCode 125. Valid Palindrome

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

  4. Java [Leetcode 125]Valid Palindrome

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

  5. [leetcode]125. Valid Palindrome判断回文串

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

  6. LeetCode 125 Valid Palindrome(有效回文)(*)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  7. Leetcode 125 Valid Palindrome 字符串处理

    题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...

  8. Java for LeetCode 125 Valid Palindrome

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

  9. [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 ...

  10. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

随机推荐

  1. JSSDK实现微信自定义分享---java 后端获取签名信息

    一.首先说下关于微信Access_token的问题,微信Access_token分为2中: 1.授权token获取方式: 这种token需要code值(如何获取code值查看官方文档) "h ...

  2. 实验:利用ASMLib创建ASM磁盘

    环境:RHEL 6.5 + Oracle 11.2.0.4 RAC(2 nodes) 目的:在实验环境使用ASMLib配置共享ASM磁盘,虽然我们已经不建议使用ASMLib进行绑盘,但是无奈有客户是这 ...

  3. Gradle入门--基本配置

    Gradle配置: Gradle构建脚本 build.gradle Gradle属性文件 gradle.properties Gradle设置文件 settings.gradle build.grad ...

  4. java1.8十大新特性详解

    "Java is still not dead-and people are starting to figure that out." 本教程将用带注释的简单代码来描述新特性,你 ...

  5. angular2+ionic2架构介绍

    不要用angular的语法去写angular2,有人说二者就像Java和JavaScript的区别.   1. 项目所用:angular2+ionic2+typescript 2. 项目结构 3. S ...

  6. mysql 索引列为Null的走不走索引及null在统计时的问题

    要尽可能地把字段定义为 NOT NULL,即使应用程序无须保存 NULL(没有值),也有许多表包含了可空列(Nullable Column)这仅仅是因为它为默认选项.除非真的要保存 NULL,否则就把 ...

  7. Zookeeper和Chubby【分布式协调系统】

    前言(对于协调系统来说其客户端往往是分布式集群) 大规模分布式系统需要解决各种类型的协调需求: 当集群中有新的进程或服务器加入时,如何探测到它的加入?如何能够自动获取配置参数? 当配置信息被某个进程或 ...

  8. Docker部署Zabbix+Grafana监控

    Docker部署Zabbix+Grafana监控 环境 centos 7 ; Docker 17.12.0-ce ; docker-compose version 1.20.1 2018-4-1 当前 ...

  9. Java常用集合学习总结

    一  数组 数组可以存储基本数据类型和对象的一种容器,长度固定,所以不适合在对象数量未知的情况下使用. Arrays : 用于操作数组对象的工具类,里面都是静态方法. Arrays.asList:把A ...

  10. Android源码解析——Toast

    简介 Toast是一种向用户快速展示少量信息的视图.当它显示时,它会浮在整个应用层的上面,并且不会获取到焦点.它的设计思想是能够向用户展示些信息,但又能尽量不显得唐突.本篇我们来研读一下Toast的源 ...