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 ...
随机推荐
- C#系统之垃圾回收
1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...
- ACM Red and Black
有一个矩形的房间,覆盖着方砖. 每个瓷砖都是红色或黑色. 一个男人站在黑色的瓷砖上,他可以移动到四个相邻的瓷砖之一. 但他不能在红砖上移动,他只能在黑砖上移动. 编写一个程序来计算他可以通过重复上述 ...
- iOS Exception Code 之 Magic Number
https://en.wikipedia.org/wiki/Hexspeak iOS Exception Code 之 Magic Number 备忘.
- java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)
java的static关键字 java中,static修饰的成员变量和成员方法叫静态变量和静态方法,不依赖类特定的实例,被类的所有实例共享. 静态变量或类变量 和 实例变量,区别是: 静态变量在内存中 ...
- android Spinner控件详解
Spinner提供了从一个数据集合中快速选择一项值的办法.默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner ...
- VMware Tools (ubuntu系统)安装详细过程与使用
前一段时间博主在VMware虚拟机上安装了Ubuntu系统,如果还没有安装的同学可以参考博主上一篇文章:VMware Ubuntu安装详细过程. 猿友们都知道linux不太好用,如果你想将你主机Win ...
- 好用的有多种样式的搜索界面创建UISearchBar
之前看到一个别人封装的第三方PYSearch,相当于一个完整的搜索界面,今天在这里进行代码说明一下. 将PYSearch拖进项目或者使用Pods进行加库,我是直接拖进项目中进行使用 PYSearch库 ...
- Android插件化的思考——仿QQ一键换肤,思考比实现更重要!
Android插件化的思考--仿QQ一键换肤,思考比实现更重要! 今天群友希望写一个关于插件的Blog,思来想去,插件也不是很懂,只是用大致的思路看看能不能模拟一个,思路还是比较重要的,如果你有兴趣的 ...
- 剑指offer-面试题7:俩个栈实现队列(c)
- POI操作excel中的日期格式处理
转载:http://blog.csdn.net/fuxiaohui/article/details/6239925 7.3.3 POI中Excel文件Cell的类型 在读取每一个Cell的值的时候,通 ...