题目:判断一个数字字符串是否是回文串。空串认为是回文串。

思路:双指针问题,重点在于此题的很多陷进:例如,s = " " ,return true。 s = ".," , return true。

代码:修改了很多遍,终于AC , 要点在于只有当头尾两个指针都指向数字或者字母时,此时才有比较操作,否则都认为是相等的。

 public boolean isPalindrome(String s) {

         int i = 0 , j = s.length() - 1;
char left = 0 , right = 0 ;
while( i <= j ){ // 控制循环结束,所有元素都已经遍历过了。 left = s.charAt(i);
if(!ifLegal(left)){
i++;
continue;
} right = s.charAt(j);
if(!ifLegal(right)) {
j--;
continue;
} //只有当left 和 right 同时指向数字、字母时,才进行比较;其它情况都认为是相等的。
if((left != right && Math.abs(left - right) != ('a' - 'A'))) return false;
i++;
j--;
}
return true;
} public boolean ifLegal(char ch){
if(ch >= 'a' && ch <= 'z') return true;
if(ch >= 'A' && ch <= 'Z') return true;
if(ch >= '0' && ch <= '9') return true; return false;
}

[leetcode]_Valid Palindrome的更多相关文章

  1. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

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

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

  6. LeetCode Longest Palindrome

    原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...

  7. LeetCode Shortest Palindrome

    原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...

  8. leetcode@ [336] Palindrome Pairs (HashMap)

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  9. LeetCode——Valid Palindrome

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

随机推荐

  1. 图标的使用————JAVA——Swing

    public class MyImageIcon extends JFrame{    public MyImageIcon()    {    JFrame jf=new JFrame();     ...

  2. Find the Clones(字典树)

    链接:http://poj.org/problem?id=2945 Description Doubleville, a small town in Texas, was attacked by th ...

  3. java中的==和!=

    java中一般很少用到==和!=,除了用于和null比较,如: if(null==o){ } //或者 if(null!=o){ } 其他地方比较一律用equals(); 建议:写完代码后在整个项目中 ...

  4. 业务中Spring使用

    不管是MVC框架还是DAO框架,在业务场景中能够通用的个人觉得AOP是一个重点,看是不是可以合理使用,其他的框架都是基础框架 ================================== 第一 ...

  5. python 实例属性之单,双下划线

    具体区别看下面例子 class A: def __init__(self,name='Andy'): self._name = name class B: def __init__(self,name ...

  6. Hive表数据导出

    方式一: hadoop命令导出 hadoop fs -get hdfs://hadoop000:8020/data/page_views2 pv2  方式二:通过insert...directory导 ...

  7. Android2.2 API中文文档——View

    概述 java.lang.Object ↳        android.view.View 已知直接子类: AnalogClock, ImageView, KeyboardView, Progres ...

  8. Redis主从是否生效的特殊测试方法

    Redis主从的特殊测试方法 配置主从 Redis的主从设定,相较于MySQL的主从,配置起来非常简单,不必像MySQL数据库一样手动记录bin log的位置再配置,但又可以像MySQL一样,一主带多 ...

  9. python学习笔记(递归函数)

    博主看了看递归.说的简单点就是程序里面再调用程序本身,或者是方法里面再调研方法本身.或者是函数里面再调研函数本身 用于什么场景呢,博主这里是父子节点排序,父子节点的查询 直接上代码: #!/usr/b ...

  10. java多态例子

    多态存在的三个必要条件一.要有继承:二.要有重写:三.父类引用指向子类对象. 代码部分: class A { public String show(D obj) { return ("A a ...