Java [Leetcode 125]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.
解题思路:
设置两个指针,一个在字符串头部,一个在字符串尾部,分别向中间移动,遇到非字母或数字则继续向中间移动,如两个都为字母或者数字,那么则比较两者是否相同。
代码如下:
public class Solution {
public boolean isPalindrome(String s) {
int i = 0, j = s.length() - 1;
char head, tail;
if(j < 0)
return true;
while(i < j){
head = s.charAt(i);
tail = s.charAt(j);
if(!Character.isLetterOrDigit(head)){
i++;
}
if(!Character.isLetterOrDigit(tail)){
j--;
}
if(Character.isLetterOrDigit(head) && Character.isLetterOrDigit(tail)){
if(Character.toLowerCase(head) != Character.toLowerCase(tail)){
return false;
}
i++;
j--;
}
}
return true;
}
}
Java [Leetcode 125]Valid Palindrome的更多相关文章
- leetcode 125. Valid Palindrome ----- java
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 a ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...
- Java for LeetCode 125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Leetcode 125 Valid Palindrome 字符串处理
题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
随机推荐
- 【BZOJ】【2940】【POI2000】条纹
博弈论 一开始想成S-Nim了……后来发现不一样= =石子是一定得取的,但是这个铺条纹就像Crosses and Crosses一样,是可以铺到中间,左右留下空隙但是对手无处可放的…… 所以就是两道题 ...
- NYOJ-79 拦截导弹 AC 分类: NYOJ 2014-01-01 23:25 167人阅读 评论(0) 收藏
#include<stdio.h> int main(){ int num[1000]={0}; int n,m,x,y; scanf("%d",&n); wh ...
- wordpress数据库优化wp_posts表 OPTIMIZE
wordpress数据库优化wp_posts表 对 MySQL 数据记录进行插入.更新或删除时,会占有不同大小的空间,记录就会变成碎片,且留下空闲的空间.就像具有碎片的磁盘,会降低性能,需要整理,因此 ...
- hdu 4696 Answers
思路:由于c[i]要么是1,要么是2.所以当c[i]中没有1的时候就不可能得到奇数: 再就是如果m<=0,也不可能得到. 代码如下: #include<cstdio> #includ ...
- http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html
http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html http://www.cnblogs.com/langtianya/a ...
- sublime 复制黏贴等快捷键修改
在 keyboard-binding user 里 增加个人配置来覆盖默认配置 [ { "keys": ["ctrl+z"], "command&qu ...
- Error: The VPN client agent was unable to create the interprocess communication depot.
当安装Cisco AnyConnect VPN Client出现The VPN client agent was unable to create the interprocess communica ...
- python 获取当前调用函数名等log信息
import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 lineNumber = sys._getframe().f_ ...
- Git教程之创建版本库(2)
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...
- win8系统 Reflect 破解
在win8系统中,从网上下载Reflect和注册机,按照教程操作,但是在生成应答字符串的时候,没有生成字符串,很纳闷. 尝试了好几个版本,最终是在win7系统中也按照流程操作一遍.重点是在生成应答字符 ...