Leetcode_125_Valid Palindrome
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [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 ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- JAVA不可变类(immutable)机制与String的不可变性
一.不可变类简介 不可变类:所谓的不可变类是指这个类的实例一旦创建完成后,就不能改变其成员变量值.如JDK内部自带的很多不可变类:Interger.Long和String等. 可变类:相对于不可变类, ...
- ACM Piggy Bank
Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...
- Git 常用命令速查表(图文+表格)
一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...
- UE4使用UMG接口操作界面
原文链接:http://gad.qq.com/article/detail/7181131 本文首发腾讯GAD开发者平台,未经允许,不得转载 UE4的蓝图之强大让人欲罢不能,但是实际在项目的开发中,C ...
- linux下内存的统计和内存泄露类问题的定位
在产品的开发中,通过对当前系统消耗内存总量的统计,可以对产品所需内存总量进行精确的评估,从而选择合适的内存芯片与大小,降低产品的成本.在遇到内存泄露类问题时,经常会对此束手无策,本文通过对proc下进 ...
- 【Android】给Android Studio设置代理
先打开我们的Android Studio,点击工具栏的file下的settings,如下图 之后再搜索框上面输入Proxy,然后按第四步提示点击,如下图 之后就进入了设置代理的界面了,如下图 默认情况 ...
- [CSDN_Markdown]Markdown基本语法2
简介 前文 Markdown基本语法 中介绍了Markdown的基本语法,知道那些基本的语法,实际上已经可以写纯文本的博客了.对我们这群写代码的人或者将要写代码的人来说,貌似这些还不够,我们还希望能插 ...
- Swift类中如何创建一个对外只读对内可读写的属性
很简单用private修饰符,后面跟限制关键字set: class Day{ private(set) var rawValue:Int = 0 func showRawValue(){ print( ...
- 全废话SQL Server统计信息(1)——统计信息简介
当心空无一物,它便无边无涯.树在.山在.大地在.岁月在.我在.你还要怎样更好的世界?--张晓风<我在> 为什么要写这个内容? 随着工作经历的积累,越来越感觉到,大量的关系型数据库的性能问题 ...
- 关于Windows下程序执行的说明
估计有很多人首次都是通过Windows(微软的操作系统)来使用计算机的,Windows的设计导致很多人认为所有程序只要双击一下就可以被正确执行了,所以一大堆初学程序设计的童鞋就会遇到些疑问: 为什么双 ...