本文是在学习中的总结,欢迎转载但请注明出处: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. jenkins + pipeline构建自动化部署

    一.引言 Jenkins 2.x的精髓是Pipeline as Code,那为什么要用Pipeline呢?jenkins1.0也能实现自动化构建,但Pipeline能够将以前project中的配置信息 ...

  2. 将Python当作计算器

    在交互模式中,最近一个表达式的值赋给变量 _.这样我们就可以把它当作一个桌面计算器,很方便的用于连续计算.例如: >>> price = 1.25 #声明变量price >&g ...

  3. CSS布局套路

    这篇笔记的目的是记录分别应用float和flex布局的方法.主要是对遇到的问题进行总结. 1.float布局 总结: 1.1 使用float布局要清除浮动,清除的方法是,在父元素添加如下样式 .cle ...

  4. ACM Meteor Shower

    贝茜听到一场非同寻常的流星雨( meteor shower)即将来临;有报道称这些流星将撞击地球并摧毁它们所击中的任何东西.为了安全起见(Anxious for her safety), ,她发誓(v ...

  5. JavaScript Date(日期)对象

    返回当日的日期和时间 如何使用 Date() 方法获得当日的日期. getFullYear() 使用 getFullYear() 获取年份. getTime() getTime() 返回从 1970 ...

  6. Android需求之点击跳转至市场评价

    相信大家都看过APP上有一个选项"喜欢此APP?还希望您评价一下吧!",然后点击弹出选择框让你选择一个市场如: 安智市场,百度应用,豌豆荚-.然后选择其中一个后就跳转至此市场你的A ...

  7. dynamic initializer和全局变量

    "慎用全局变量,包括全局静态变量" 是众所周知的原则,因为全局变量除了会增加程序的维护成本. 如果全局变量是个复杂的对象,并且还使用其他的全局变量,那情况就变得复杂的多.因为全局变 ...

  8. 亲密接触Redis-第三天(Redis的Load Balance)

    前言 上两天讲述了Redis的基本搭建和基于HA的集群布署方式以及相关的策略和注意点.今天开始讲述Redis的Cluster功能,而这块目前来说网上资料不是太全,就算有1,2篇也只是单讲服务端的搭建也 ...

  9. PGM:图模型学习概述

    http://blog.csdn.net/pipisorry/article/details/52571640 动机 前面我们讨论的问题出发点是给定一个图模型.如在独立性和推理讨论中,假定模型--结构 ...

  10. Linux中的高级文本处理命令,cut命令,sed命令,awk命令

    1.2.1 cut命令 cut命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields    ## 用于有特定分隔字符 [r ...