Question: 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.
Example Questions Candidate Might Ask:
Q: What about an empty string? Is it a valid palindrome?
A: For the purpose of this problem, we define empty string as valid palindrome.
Solution:
O(n) runtime, O(1) space:
The idea is simple, have two pointers – one at the head while the other one at the tail.
Move them towards each other until they meet while skipping non-alphanumeric
characters.
Consider the case where given string contains only non-alphanumeric characters. This is
a valid palindrome because the empty string is also a valid palindrome.        

public boolean isPalindrome(String s) {

  int i = 0, j = s.length() - 1;

  while (i < j) {

    while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++;               //Character.asLetterOrDigit()  用来判断是不是字母或者数字
    while (i < j && !Character.isLetterOrDigit(s.charAt(j))) j--;
    if (Character.toLowerCase(s.charAt(i))!= Character.toLowerCase(s.charAt(j))) {    
      return false;

    }

    i++; j--;
  }
  return true;
}

注:这道题类似于用了两个指针在数组两头,同时,while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++ 用的很巧妙,避免了很多判断。

Leetcode 详解(valid plindrome)的更多相关文章

  1. Leetcode 详解(Valid Number)

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  2. 由Leetcode详解算法 之 动态规划(DP)

    因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...

  3. Leetcode 详解(ReverseWords)

    Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...

  4. Leetcode 详解(股票交易日)(动态规划DP)

    问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...

  5. Leetcode详解Maximum Sum Subarray

    Question: Find the contiguous subarray within an array (containing at least one number) that has the ...

  6. Leetcode 详解(Substing without repeats character)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. The Skyline Problem leetcode 详解

    class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...

  9. 访问Storm ui界面,出现org.apache.storm.utils.NimbusLeaderNotFoundException: Could not find leader nimbus from seed hosts ["master"]. Did you specify a valid list of nimbus hosts for confi的问题解决(图文详解)

    不多说,直接上干货! 前期博客 apache-storm-0.9.6.tar.gz的集群搭建(3节点)(图文详解) apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)( ...

随机推荐

  1. 解决对含有第三方jar包的项目打包出现java.lang.NoClassDefFoundError问题

    用eclipse普通的打包方式,对含有第三方jar包的项目进行打包.调用方法后一只出现java.lang.NoClassDefFoundError问题. 从网上搜寻,很多都是在MANIFEST.MF文 ...

  2. JAVA 多线程学习总结

    新手一枚,Java学习中,把自己学习多线程的知识总结一下,梳理下知识,方便日后查阅,高手莫进. 本文的主要内容: [1]    实现线程的两种方法                [2]  线程的启动与 ...

  3. 死性不改ISO9000系列系统 2011q4~2016q1

    百度云链接: http://pan.baidu.com/s/1o8rO3W2 密码: kabg 2016q2由于发布没多久,可能会重新修改,暂时不转载.

  4. 从零开始学习Android(一)Android环境的搭建

    好久没有开始写博客了,最近开始学习Android,所以想把学习的笔记都一一记录下来.一来是方便自己以后资料的查询,其次也是给Android新手朋友进行学习使用,再次也希 望得到高手的指点.废话少说,我 ...

  5. 【Python】Python3中的str和bytes

    参考文章:Python 3的bytes/str之别 len()函数计算的是str的字符数,如果换成bytes,len()函数就计算字节数 >>> len('ABC') 3 >& ...

  6. mac os中配置多个jdk(转载+)

    1.首先安装所有的JDk: * Mac自带了的JDK6,安装在目录:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/下. * JDK7,JDK8则 ...

  7. Docker常见问题解决

    一.Docker中运行ubuntu容器,字体颜色太深导致无法看清 解决方案: 1. 利用dircolors命令,查看我们的系统当前的文件名称显示颜色的值,然后利用管道重定向到用户目录下的任意一个文件( ...

  8. 月薪3万的程序员告诉你:这样工作才能拿高薪(转 IT之家)

    习惯即刻回报 他不懂得只有春天播种,秋天才会有收获.刚刚付出一点点,甚至还没有付出,就想要得到回报.技术刚刚掌握,能一边百度一边干活了就觉得该拿到多少多少钱了.找工作先想着多少多少钱,入职了没干几个月 ...

  9. 别不信!App三年内将被HTML5顶替彻底消失?

    2007年W3C(万维网联盟)立项HTML5,直至2014年10月底,这个长达八年的规范终于正式封稿. 过去这些年,HTML5颠覆了PC互联网的格局,优化了移动互联网的体验,接下来,HTML5将颠覆原 ...

  10. 【oracle】oracle学习笔记2--scoot账户的激活与解锁

    1.用sysz账户as sysddba登录,并执行scott.sql文件.我的文件目录是C:\oraclexe\app\oracle\product\11.2.0\server\rdbms\admin ...