【LeetCode】- 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: ]
先把有效的字母、数字准备好,然后遍历目标字符串,有效的字符放入buffer。
再比較buffer中的字符串和反转后的字符串,假设同样那就是回文。否则不是回文。
点评:这个解法能够被系统accept,但调用了太多api,性能一般。
public class Solution { public boolean isPalindrome(String s) {
StringBuilder buffer = new StringBuilder();
String tempStr = "abcdefghijklmnopqrstuvwxyz0123456789";
char[] cArr = s.toCharArray();
for (int i = 0; i < cArr.length; i++) {
if (tempStr.contains(String.valueOf(cArr[i]).toLowerCase())) {
buffer.append(String.valueOf(cArr[i]).toLowerCase());
}
}
String currentStr = buffer.toString();
String reverseStr = buffer.reverse().toString();
if (currentStr.equals(reverseStr)) {
return true;
}
return false;
} }
[ 解法2: ]
採用二分法,一个指针从左边遍历,一个从右边遍历,跳过非字母和非数字,当遍历到中点依旧同样那就是回文
点评:代码清晰。性能较好。
public class Solution { /**
* 推断是否是回文
*
* @param String str
* @return boolean true(is palindrome)/false(is not palindrome)
*/
public boolean isPalindrome(String s) {
if (s == null) {
return false;
} int i = 0;
int j = s.length() - 1;
while (i < j) {
if (!isAlphanumeric(s.charAt(i))) {
i++;
continue;
}
if (!isAlphanumeric(s.charAt(j))) {
j--;
continue;
}
if(Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(s.charAt(j))){
i++;
j--;
continue;
}
return false;
}
return true;
} /**
* 推断是否是字母或数字
*
* @param char character
* @return boolean true(is alphanumeric) / false(is not alphanumeric)
*/
public boolean isAlphanumeric(char c) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
return true;
}
return false;
} }
版权声明:本文博主原创文章,博客,未经同意不得转载。
【LeetCode】- Valid Palindrome(右回文)的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
随机推荐
- VB.NET 机房收费系统项目总结
VB.NET机房收费系统项目总结 从2013年5月3日——2013年8月20日历时三个多月的.NET机房收费系统终于完成了.项目做完了,真有一种如释重负的感觉. 下面我将从文档.UML图,代码这三个方 ...
- fzu2150(bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 题意:在任意两处点火,求最短时间烧光所有草堆. 分析:由于n,m比较小,将所有草堆坐标记录下来,然后暴力 ...
- HDU 1226 超级密码 (搜素)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1226 题意简单,本来是一道很简单的搜素题目. 但是有两个bug: 1.M个整数可能有重复的. 2.N可 ...
- HTML5拖动画布/拖放
<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> func ...
- oracle varchar2 和varchar 区别
今天,往ORACLE一个表的VACHAR2(20)字段中插入七个汉字,提示错误:插入的值太大. 改成插入六个汉字,又可以. 一直以来,都以为一个汉字占两个字节.觉得非常奇怪. 用length().le ...
- 物联网 开发板 基于ESP8266
The ESP8266 The ESP8266 is a highly integrated chip designed for the needs of an increasingly connec ...
- java的提取与替换操作
public class Demo02 { public static void main(String args[]){ String str = "java 技术学习班 2007032 ...
- linuxserver启动过程
随着Linux的应用日益广泛.特别是在网络应用方面,有大量的网络server使用Linux操作系统.因为Linux的桌面应用和Windows相比另一 定的差距.所以在企业应用中往往是Linux和Win ...
- 安装配置gerrit
Centos 安装配置gerrit 关闭selinux,不然nginx的反向代理会报错connect() to 127.0.0.1:8080 failed (13: Permission denied ...
- Nginx将请求分发到各web应用
介绍了VMWare12虚拟机.Linux(CentOS7)系统安装.部署Nginx1.6.3代理服务做负载均衡.接下来介绍通过Nginx将请求分发到各web应用处理服务. 一.Web应用开发 1.as ...