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 ...
随机推荐
- Chrome浏览器下调试SASS
网上说的方案各有各的说法,尝试多个才找到有效方案,为避免后来者麻烦,现在讲讲如何调试 笔者ruby版本为3.4.19,因为sass文件的编译是需要ruby环境的,使用sass前需要先安装! 第一步(核 ...
- 项目中的libevent
单线程libevent模式 项目里面是多线程版的,我先理解下单线程的. //client .调用NGP::init() bool NGP::init(NGPcontext context) { _co ...
- matlab fscanf用法
matlab fscanf用法 matlab中的fscanf的用法如下: A=fscanf(fid,format)[A, count]=fscanf(fid,format,size) [A, coun ...
- hdoj 1879 继续畅通工程
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- C#扩展方法入门
扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀. 仅当你使用 using 指令将命名空间显式导入到源代码 ...
- js注册验证【转】
function getFocus() //设置用户名文本框获取焦点 { document.getElementById("txtuname").focus(); } functi ...
- Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
异常描述: 在对HDFS格式化,执行hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示: [shirdrn@localhost bin]$ hadoop n ...
- lintcode:Valid Sudoku 判断数独是否合法
题目: 判断数独是否合法 请判定一个数独是否有效.该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...
- lintcode:Plus One 加一
题目: 加一 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组. 该数字按照大小进行排列,最大的数在列表的最前面. 样例 给定 [1,2,3] 表示 123, 返回 [1,2,4 ...
- 用JUnit4进行单元测试
转载:http://tonl.iteye.com/blog/1948869 参考: http://thihy.iteye.com/blog/1771826 http://developer.51cto ...