本文是在学习中的总结,欢迎转载但请注明出处: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. opencv 3.1.0 访问像素值的三种方法(C++)

    三种方法分别问: 指针访问:void colorReduce_ptr(cv::Mat &inputImage, cv::Mat &outputImage, int div); 迭代器访 ...

  2. 解决问题redis问题:ERR Client sent AUTH, but no password is set

    是的,这又是一个坑爹的问题. 明明在redis.conf中设置了密码,而且redis还启动了,为什么说没有密码呢? 大家都知道linux下启动redis有很多种方法, 其中有 ./redis-serv ...

  3. JAVA中抽象类的使用

    抽象类是从多个具体类中抽象出来的父类,它具有更高层次的抽象.抽象类体现的就是一种模板模式的设计,抽象父类可以只定义需要使用的某些方法,把不能实现的某些部分抽象成抽象方法,留给其子类去实现.具体来说,抽 ...

  4. Python3 元组

    Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 = ('Goog ...

  5. C++用LuaIntf调用Lua代码示例

    C++用LuaIntf调用Lua代码示例 (金庆的专栏 2016.12) void LuaTest::OnResponse(uint32_t uLuaRpcId, const std::string& ...

  6. springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore

    @SessionAttribute作用于处理器类上,用于在多个请求之间传递参数,类似于Session的Attribute,但不完全一样,一般来说@SessionAttribute设置的参数只用于暂时的 ...

  7. APP自动化框架LazyAndroid使用手册(4)--测试模板工程详解

    概述 前面的3篇博文分别对lazyAndroid的框架简介.元素抓取和核心API进行了说明,本文将基于框架给出的测试模板工程,详细阐述下使用该框架进行安卓UI自动化测试的步骤. 模板工程 先来看一下模 ...

  8. 最优秀的网络框架retrofit

    由于某学员要求所以我打算写一篇 标题先记录下来 我会在一周内完成此篇文章

  9. 计算机网络之域名系统DNS

    域名系统DNS 域名系统DNS(Domai NameSystem)是因特网使用的命名系统,用于把便于人们使用的机器名字转换为IP地址. 许多应用层软件经常直接使用域名系统,但计算机的用户只是间接而不是 ...

  10. 【SSH系列】---Hibernate的基本映射

    开篇前言       在前面的博文中,小编分别介绍了[SSH系列]-- hibernate基本原理&&入门demo,通过这篇博文,小伙伴们对hibernate已经有了基本的了解,以及h ...