本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41488377

Valid Palindrome

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.

通过本文你可能会学到的知识为:

(1)对String类中的valueOf()方法、charAt()方法、equalsIgnoreCase()方法有所了解,并知道如何使用。

(2)对Character类中的isLetterOrDigit()方法有所了解。

(3)理解解题思路,提高分析问题的能力。

注:

String类: valueOf()方法——返回指定类型参数的字符串表示形式。

charAt(char c)方法——返回指定索引处的 char 值。

equalsIgnoreCase()方法——对两个String进行比较,不考虑大小写。

Character类:

isLetterOrDigit(char c)方法——确定指定字符是否为字母或数字。

思路:

(1)解读题意:1. 需要去掉非数字和非字母字符,对剩余字符串是否为回文串进行判断。说白了就是判断该字符串是否轴对

称。2. 空字符串是回文的。3.只含有标点或其它特殊符号的字符串是回文的。

(2)既然是判断字符串是否轴对称,那么,分别从字符串起始位置和结束位置开始进行比较,对于开始字符或者结束字符,首

先判断其是否为数字或字母,如果不是,对于开始位置,则继续向右寻找第一个是字母或者数字的字符,对于结束位置,则继续

向左寻找第一个是字母或者数字的字符,如果从起始位置向右或者结束位置向左遍历完整个字符串还未找到第一个出现的数值为

字母或数字的字符,则返回true。

(3)如果起始位置对应字符和结束位置对应字符都是字母或者数字,但数值不同,则不是回文字符串,返回false;如果相同,

则起始位置右移,结束位置左移,继续按照(2)进行判断,依此类推,如果都判断完成且没有返回false,那么说明字符串为回文

串,返回true。

算法实现代码如下所示:(PS:本人技术有限,目前尚不能写出简短高效的代码,大家有好的算法希望能够分享,谢谢)

  1. public boolean isPalindrome(String s) {
  2. if (s.length() == 0) return true;
  3. int len = s.length();
  4. int start = 0;
  5. int end = len - 1;
  6.  
  7. while (start != end && start < end) {
  8. String s1 = "";
  9. String s2 = "";
  10.  
  11. while (start < len) {
  12. if (Character.isLetterOrDigit(s.charAt(start)) != true) {
  13. start++;
  14. } else {
  15. s1 = String.valueOf(s.charAt(start));
  16. break;
  17. }
  18. }
  19.  
  20. if (start == len - 1
  21. && Character.isLetterOrDigit(s.charAt(start)) == true)
  22. return true;
  23.  
  24. while (end > 0) {
  25. if (Character.isLetterOrDigit(s.charAt(end)) != true) {
  26. end--;
  27. } else {
  28. s2 = String.valueOf(s.charAt(end));
  29. break;
  30. }
  31. }
  32.  
  33. if (end == 0 && Character.isLetterOrDigit(s.charAt(end)) == true)
  34. return true;
  35.  
  36. if (s1.equalsIgnoreCase(s2)) {
  37. start++;
  38. end--;
  39. } else {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }

Leetcode_125_Valid Palindrome的更多相关文章

  1. PALIN - The Next Palindrome 对称的数

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  2. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  3. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  4. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  5. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  6. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  7. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. 模仿天猫实战【SSM版】——后台开发

    上一篇文章链接:模仿天猫实战[SSM版]--项目起步 后台需求分析 在开始码代码之前,还是需要先清楚自己要做什么事情,后台具体需要实现哪些功能: 注意: 订单.用户.订单.推荐链接均不提供增删的功能. ...

  2. RunLoop总结:RunLoop 与GCD 、Autorelease Pool之间的关系

    如果在面试中问到RunLoop相关的知识,很有可能也会问到RunLoop与GCD.Autorelease Pool有没有关系,哪些地方用到了GCD.Autorelease Pool等. So,本文就总 ...

  3. 临时关闭Mac SIP系统完整性保护机制

    # 修正更新 [2016-12-27] 晚上给我笔记本安装的时候,使用user权限安装成功,mac最后是关闭sip才安装成功. $ pip install -r requirements.txt -- ...

  4. SQL Server 虚拟化(2)——理想的SQL Server虚拟机架构

    本文属于SQL Server虚拟化系列 搭建SQL Server虚拟机,在各个组织之间都有自己的标准和最佳实践.从第一眼看去,光物理配置就有过百种,所有的这些细微差别都有可能为后续日常管理过程中故障侦 ...

  5. 用scheme最基本的元素定义排序函数

    用到的元素有9个: define,if,null?,cons car,cdr,lambda,let,named let, 其实let 和 named let可以去掉.但那样会带来性能和可读性下降的问题 ...

  6. [LaTex]插图

    1.不错的Latex参考网站 http://www.ctex.org/documents/latex/graphics/node120.html http://www.ctex.org/documen ...

  7. SQL Server 索引维护(1)——如何获取索引使用情况

    前言: 在前面一文中,已经提到了三类常见的索引问题,那么问题来了,当系统出现这些问题时,该如何应对? 简单而言,需要分析现有系统的行为,然后针对性地对索引进行处理: 对于索引不足的情况:检查缺少索引的 ...

  8. 并发计算模型BSP与SEDA

    1    BSP批量同步并行计算 BSP(Bulk Synchronous Parallel)批量同步并行计算用来解决并发编程难的问题.名字听起来有点矛盾,又是同步又是并行的.因为计算被分组成一个个超 ...

  9. JDBC-数据库的更新操作编程(三)

    首先建立一个静态方法,代码如下: public static Statement getStatement(){ Statement st = null; try { Class.forName(&q ...

  10. UNIX网络编程——tcp流协议产生的粘包问题和解决方案

    我们在前面曾经说过,发送端可以是一K一K地发送数据,而接收端的应用程序可以两K两K地提走数据,当然也有可能一次提走3K或6K数据,或者一次只提走几个字节的数据,也就是说,应用程序所看到的数据是一个整体 ...