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

思路:双指针问题,重点在于此题的很多陷进:例如,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. C++学习8 构造函数的参数初始化表

    构造函数是一种特殊的成员函数,在创建对象时自动执行,主要用来进行初始化工作,例如对 private 属性的成员变量赋值. 对成员变量的初始化,除了在构造函数的函数体中一一赋值,还可以采用参数初始化表. ...

  2. SQL 去特殊字符

    )) ) as begin declare @i int while patindex('%[^%@+*,=../_ <>''" ^0-9 ^a-Z ^''- ^吖-座]%' , ...

  3. jquery on off 方法

    $("p").on("click",function(){alert("The paragraph was clicked.");}); $ ...

  4. Aspose.Cells单元格转换为数字格式

    需要在两个地方设置 //Adding a numeric value to "A2" cell "; worksheet.Cells["A2"].Pu ...

  5. CentOS 6.5 安装 Nginx 1.7.8 教程

    http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29791971&id=4702007 Nginx是一款轻量级的Web ...

  6. 算法库:基础线性代数子程序库(Basic Linear Algebra Subprograms,BLAS)介绍

    调试DeepFlow光流算法,由于作者给出的算法是基于Linux系统的,所以要在Windows上运行,不得不做大量的修改工作.移植到Windows平台,除了一些头文件找不到外,还有一些函数也找不到.这 ...

  7. C语言中的atan和atan2(转)

    在C语言的math.h或C++中的cmath中有两个求反正切的函数atan(double x)与atan2(double y,double x)  他们返回的值是弧度 要转化为角度再自己处理下. 前者 ...

  8. .NET基础操作回顾_使用ADO.NET操作SqlServer使用的类

    有些工具用的久了或者有新工具出现后,就慢慢的遗忘了很多,它们从熟悉的变成陌生,当然,对于我们来说不是好事吧. 今天回顾一下ADO.NET用到的MS的基础类库,先上代码(标准的SqlServer操作) ...

  9. 面向对象的ExtJS场景开发

    写ExtJS已经3各月了,项目中临时学的,主要参考ExtJS 的文档学习,推荐一款JS开发工具Aptana Studio 3. 大概说一下开发ExtJS的准备: 1.下载Extjs(目前有4.x我使用 ...

  10. 模拟一下goldengate中断后,重新同步操作

    模拟一下goldengata中断后,重新同步操作:     1.关掉源端抽取进程 GGSCI (20081122-2105) 15> info all Program     Status    ...