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

思路:双指针问题,重点在于此题的很多陷进:例如,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. dede织梦列表页如何调用全站子栏目

    网站原代码:{dede:channel type='son'} <a href="[field:typelink/]">[field:typename/]</a& ...

  2. java io操作常规

    1.四种类型: 字节流: InputStream OutputStream 字符流: Reader Writer 2.转换流 InputStreamReader, OutPutStreamWriter ...

  3. (easy)LeetCode 232.Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. [ACDream 1430]SETI 后缀数组

    题目链接:http://acdream.info/problem?pid=1430 题目大意:给你一个长度不超过10000的字符串,问你出现过两次或两次以上的不重叠的子串有多少个. 后缀数组计算出he ...

  5. Flexigrid例子二: 原位编辑器

    有时候,我们想要编辑flexigrid里的数据.一个原位编辑器是需要的,现在不需要再弹出一个对话框了.这里我会展示如何做到这点. 我使用了jquery-in-place-editor库.请参考官方站点 ...

  6. DFS

    HDU1181  http://acm.hdu.edu.cn/showproblem.php?pid=1181 #include<stdio.h> #include<algorith ...

  7. 慕课网-安卓工程师初养成-2-9 Java中的自动类型转换

    来源:http://www.imooc.com/code/1236 在 Java 程序中,不同的基本数据类型的数据之间经常需要进行相互转换.例如: , 代码中 int 型变量 score1 可以直接为 ...

  8. break 和 continue

    break 和 continue 相同点: 都 用在循环体内,如 switch.for.while.do while的程序块中,用于控制程序循环语句的执行 不同点: break可以离开当前switch ...

  9. 【spring 3】AOP:静态代理

    一.代理的基本简介 首先,在什么时候使用代理: 在面向方面编程过程中,当需要对所有类进行某种操作(如,安全性检查,记录操作日志)时,考虑到OCP原则,我们不能在所有实现类中直接添加某些相关方法,这样一 ...

  10. angular factory Services provider 自定义服务 工厂

    转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1602.html 1.在app.js 中声明了模块的依赖 var phonecatApp = angular ...