题目

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;
         }

最新文章

  1. c# Using Settings under visual studio 2012
  2. vagrant学习笔记
  3. 使用SOUI开发的界面集锦
  4. BZOJ3413 : 匹配
  5. 伪静态&lt;-&gt; 动态页,伪静态,真静态的比较和选择
  6. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
  7. CentOS6.8通过yum安装MySQL5.7
  8. 【BZOJ1924】【SDOI2010】所驼门王的宝藏(Tarjan,SPFA)
  9. JQuery设置checkbox的值,取checkbox的值,设置radio的值,取radio的值,设置下拉选select的值,取select的值
  10. 实时显示数据 SignalR 及时消息提醒( 立即向其推送内容)
  11. 配置webpack loader vue 报错:Module build failed: TypeError: this._init is not a function
  12. window下的Django入门
  13. 分类算法的R语言实现案例
  14. 最简单的DHCP服务
  15. redis知识积累
  16. 使用Google-Colab训练PyTorch神经网络
  17. Alpha阶段敏捷冲刺⑦
  18. sysctl -P 报错解决办法
  19. java 普通容器,同步容器,并发容器,同步工具
  20. Win32 基本文件读写操作

热门文章

  1. 【BZOJ 3028】 3028: 食物 (生成函数)
  2. android 流行框架的使用
  3. android的AsyncTask.get()方法会阻塞UI线程
  4. SQL Server Management Studio 教程二: 创建新登录名
  5. android tesseract-ocr实例教程(包含中文识别)(附源码)
  6. 使用 IntraWeb (3) - 页面切换
  7. Linux下查看哪些IP登陆过系统/var/log/wtmp
  8. 反接保护电路 Reverse Voltage Protection
  9. Eclipse Mark Occurrences
  10. 【Go入门教程2】基本构成元素:标识符(identifier)、关键字(keyword 25个)、字面量(literal)、分隔符(delimiter)、和 操作符(operator)