Valid Palindrome(LintCode)
2024-09-23 23:22:15
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
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.
Challenge
O(n) time without extra memory.
深刻体会到了英文的用处。。。题目的中文翻译真是误导人。
public class Solution {
/**
* @param s A string
* @return Whether the string is a valid palindrome
*/
public boolean isPalindrome(String s) {
char[] cs = s.toCharArray();
int i = 0;
int j = cs.length - 1; while(i<j) {
while(i<j && !Character.isDigit(cs[i]) && !Character.isLetter(cs[i])) i++;
while(i<j && !Character.isDigit(cs[j]) && !Character.isLetter(cs[j])) j--;
if(i >= j) break;
String ss = cs[i] + "";
String ss1 = cs[j] + "";
if(!ss.equalsIgnoreCase(ss1))
return false; i++;
j--;
}
return true;
}
}
最新文章
- 关于.9.png格式图片的制作与使用
- sql server 常见错误代码15000 - 15999含义解析
- 汇编学习(一)&mdash;&mdash;win7 64位调出debug
- HackerRank ";Minimum Penalty Path";
- gerrit 调试smtp email
- 使用Varnish代替Squid做网站缓存加速器的详细解决方案----转载
- sqlserver 字符串相关函数
- HDU4349--Xiao Ming&#39;s Hope(数论)
- Linux下搭建Oracle11g RAC(1)----IP分配与配置IP
- Linux下得到显示屏参数的方法
- [转]ActiveMQ 即时通讯服务 浅析
- 環氧樹脂對COB的影響
- Java网络编程:利用apache的HttpClient包进行http操作
- Angularjs,WebAPI 搭建一个简易权限管理系统
- java练习 - 字符串反转
- 《java.util.concurrent 包源码阅读》21 CyclicBarrier和CountDownLatch
- js与jQuery的区别——每日一记录
- mysql---select的五种子句学习(where、group by、having、order by、limit)
- Java学习笔记47(JDBC、SQL注入攻击原理以及解决)
- 20155302 2016-2017-2 《Java程序设计》第九周学习总结