题目

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.

题解

这道题的几个点,

一就是alphanumeric characters and ignoring cases,字母和数字,忽略大小写。

二就是考虑空字符串是否为回文,最好在面试时候问下面试官,这里是认为空字符串是回文。

因为忽略大小写,所以就统一为大写。

然后就判断当前检查字符是否符合范围,否则大小指针挪动。

如果发现有大小指针指向的值有不同的,就返回false,否则,继续检查。

最后返回true。

代码如下:

 1        public static boolean isPalindrome(String s) {
 2             if(s.length()==0)
 3                 return true;
 4             
 5             s = s.toUpperCase();
 6             int low1 = 'A', high1 = 'Z';
 7             int low2 = '0', high2 = '9';
 8             int low = 0, high = s.length()-1;
 9             
             while(low < high){
                 if((s.charAt(low)<low1||s.charAt(low)>high1)
                     && (s.charAt(low)<low2||s.charAt(low)>high2)){
                         low++;
                         continue;
                     }
                     
                 if((s.charAt(high)<low1||s.charAt(high)>high1)
                     && (s.charAt(high)<low2||s.charAt(high)>high2)){
                         high--;
                         continue;
                     }
                 if(s.charAt(low) == s.charAt(high)){
                     low++;
                     high--;
                 }else
                     return false;
             }
             return true;
         }

Valid Palindrome leetcode java的更多相关文章

  1. Valid Palindrome [LeetCode]

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

  2. LeetCode算法题-Valid Palindrome(Java实现)

    这是悦乐书的第174次更新,第176篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第33题(顺位题号是125).给定一个字符串,确定它是否是回文,只考虑字母数字字符并忽略 ...

  3. Valid Palindrome ---- LeetCode 125

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

  4. Valid Sudoku leetcode java

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  5. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  6. Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  7. Valid Number leetcode java

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

  8. [Leetcode][JAVA] Valid Palindrome

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

  9. leetcode 125. Valid Palindrome ----- java

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

随机推荐

  1. 在ssh中利用Solr服务建立的界面化站内搜索

         继上次匆匆搭建起结合solr和nutch的所谓站内搜索引擎之后,虽当时心中兴奋不已,可是看了看百度,再只能看看我的控制台的打印出每个索引项的几行文字,哦,好像差距还是有点大……      简 ...

  2. android studio 查看大纲

    就是 structure 面板 快捷键 Alt+7 === android studio 查看方法说明 点击菜单“View”-“Quick Documentation" 建议直接查看源代码文 ...

  3. Bzoj5209[Tjoi2012]防御:姿势题

    首先这题现在在BZOJ上是没数据的,你可以选择python2B获得AC,也可以去洛谷上交.选择第一个选项的现在可以不用看了...... 关于这题的题意,击破的一次攻击即使溢出也不双倍,否则你过不了样例 ...

  4. [Java]Get与Post,客户端跳转与服务器端跳转

    http://www.thinksaas.cn/group/topic/133101/ 虽然说get 与post 问题很老套了,但是作为web 开发人员来说对于这个的理解确实很有必要,其实说到get  ...

  5. LightOJ 1118 - Incredible Molecules (两圆面积交)

    1118 - Incredible Molecules   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: ...

  6. 《Go语言实战》摘录:6.3 并发 - 竞争状态

    6.3 并发 - 竞争状态

  7. Golang Vendor 包管理工具 glide 使用教程

    Glide 是 Golang 的 Vendor 包管理器,方便你管理 vendor 和 verdor 包.类似 Java 的 Maven,PHP 的 Composer. Github:https:// ...

  8. 使用React改版网站

    网站是毕业设计的作品,开发这个网站的目的主要用于记录一些笔记,以及聚合一些资讯信息,也算自己在网络世界中的一块静地吧,可以在这里一些技术上想法的实践. 网站最初前端使用vue开发,在前段时间由于项目的 ...

  9. pio 背景色

    This example shows you Excel cell fills and colors using Apache POI. In our example i have used all ...

  10. 本地docker搭建gitlab, 并配置ldap认证

    基于Docker在Mac OS X系统中的部署和设置GitLab的具体过程如下:   1. 安装Docker for Mac (参见https://docs.docker.com/docker-for ...