本文是在学习中的总结,欢迎转载但请注明出处: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:本人技术有限,目前尚不能写出简短高效的代码,大家有好的算法希望能够分享,谢谢)

public boolean isPalindrome(String s) {
	if (s.length() == 0) return true;
	int len = s.length();
	int start = 0;
	int end = len - 1;

	while (start != end && start < end) {
		String s1 = "";
		String s2 = "";

		while (start < len) {
			if (Character.isLetterOrDigit(s.charAt(start)) != true) {
				start++;
			} else {
				s1 = String.valueOf(s.charAt(start));
				break;
			}
		}

		if (start == len - 1
				&& Character.isLetterOrDigit(s.charAt(start)) == true)
			return true;

		while (end > 0) {
			if (Character.isLetterOrDigit(s.charAt(end)) != true) {
				end--;
			} else {
				s2 = String.valueOf(s.charAt(end));
				break;
			}
		}

		if (end == 0 && Character.isLetterOrDigit(s.charAt(end)) == true)
			return true;

		if (s1.equalsIgnoreCase(s2)) {
			start++;
			end--;
		} else {
			return false;
		}
	}
	return true;
}

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. html css <input> javaScript .数据类型 JS中的函数编写方式 BOM总结 DOM总结

    Day27  html css div 块标签. 特点: 独占一行,有高度和宽度 span 行元素. 特点:在同一行显示,当前行满了自动去下一行显示. 不识别高度和宽度 1.1.1.1 2.输入域标签 ...

  2. python学习之路网络编程篇(第一篇)socket初识

    什么是socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为socket.socket通常也称为“套接字”,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的 ...

  3. Android开发学习之路--性能优化之布局优化

      Android性能优化方面也有很多文章了,这里就做一个总结,从原理到方法,工具等做一个简单的了解,从而可以慢慢地改变编码风格,从而提高性能. 一.Android系统是如何处理UI组件的更新操作的 ...

  4. Swift基础之如何使用iOS 9的Core Spotlight框架

    本文由CocoaChina译者KingOfOnePiece(博客)翻译 作者:GABRIEL THEODOROPOULOS?校对:hyhSuper 原文:How To Use Core Spotlig ...

  5. J2EE进阶(十七)Hibernate中常用的HQL查询方法(getHibernateTemplate())

    J2EE进阶(十七)Hibernate中常用的HQL查询方法(getHibernateTemplate())   当我们使用Hibernate进行数据的CRUD操作时,利用模版进行操作不失为一种方法. ...

  6. Android图表库MPAndroidChart(十一)——多层级的堆叠条形图

    Android图表库MPAndroidChart(十一)--多层级的堆叠条形图 事实上这个也是条形图的一种扩展,我们看下效果就知道了 是吧,他一般满足的需求就是同类数据比较了,不过目前我还真没看过哪个 ...

  7. springMVC源码分析--RequestMappingHandlerAdapter(五)

    上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...

  8. Linux动态频率调节系统CPUFreq之二:核心(core)架构与API

    上一节中,我们大致地讲解了一下CPUFreq在用户空间的sysfs接口和它的几个重要的数据结构,同时也提到,CPUFreq子系统把一些公共的代码逻辑组织在一起,构成了CPUFreq的核心部分,这些公共 ...

  9. HTML简单使用

    HTML简单使用 标签 : 前端技术 HTML HTML(Hypertext Marked Language), 即超文本标记语言,能够独立于各种操作系统平台(如UNIX/Linux/Windows等 ...

  10. [Mysql]Innodb 独立表空间和共享表空间

    innodb有2中表空间方式: 共享表空间 和 独立表空间 查询数据的设置: show variables like '%per_table'; 默认是共享表空间,独立表空间在配置文件中添加 inno ...