题目

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. span 居中

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha ====== 把span 升级为块级元素,div 的宽度.然后使用定位,添加 text- ...

  2. 简单分享apache封IP的方法

    1. 在配置文件里设置: 打开httpd.conf编辑:<Directory “/var/www/html”>     Options Indexes FollowSymLinks    ...

  3. MCU PWM DAC OP Voltage Output

  4. STM32定时器级联 -- AN2592

    Master configuration When a timer is selected as a master timer, the corresponding trigger output si ...

  5. latex编写论文

    写给像我这样需要使用latex编写论文的小菜鸟,给出demo和注释,高级部分自己参透(默认你已经搭好环境). 1.搭论文架子 demo1 \documentclass[10pt,a4paper]{ar ...

  6. WebLogic使用总结(四)——WebLogic部署Web应用

    一.打包Web应用 首先将要部署到WebLogic的Web应用打包成war包,具体操作步骤如下图所示: 选中要打包的[oams]项目→[Export...]

  7. 解决ubuntu上在androidstudio中启动emulator闪退的问题(1)

    作者 彭东林 pengdonglin137@163.com 平台 Ubuntu14.04 64 androidstudio 2.3.3 现象 在创建好模拟器后,点击启动时,模拟器界面刚出来就闪退了 解 ...

  8. AngularJS中Directive间交互实现合成

    假设需要烹饪一道菜肴,有3种原料,可以同时使用所有的3种原料,可以使用其中2种,也可以使用其中1种. 如果以Directive的写法,大致是:<bread material1 material2 ...

  9. C#编程(三十六)----------元组

    元组 数组合并了相同类型的对象,而元组合并了不同类型的对象. .NET 4定义了8个泛型Tuple类和一个静态的Tuple类,他们用作元组的工厂. 元组是一种数据结构,通过逗号分隔 Tuple< ...

  10. Linux netstat命令具体解释

    简单介绍 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表.接口状态 (Interface Statistics).masquerade 连接,多播成员 (Multicast Memb ...