Lintcode415-Valid Palindrome-Medium
Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ignoring cases.
Example
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama"
Example 2:
Input: "race a car"
Output: false
Explanation: "raceacar"
Challenge
O(n) time without extra memory.
Notice
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.
思路:
字符串中只考虑字母和数字是否构成一个回文串,no extra space可以定义两个指针。
注意:
- 两个指针命名,front 和 end,不要用i, j
- 同类中:方法与方法之间直接用方法名来互相调用。Java方法的调用。
- 多个if并列 和 else if 区别:If you have used multiple
ifstatements then if the condition istrueall will be executed. If you have usedifandelse ifcombination only one will be executed where first comes the true value
所以,如果把line 10 12 14 变成并列if,output会出错。因为如果连续出现两个或以上的非字母数字字符的话,就会输出false。比如 String s = a'+ba; - boolean方法命名以is开头。
- line 9, 不能写成 while (front != end) , 举例 s = "aa", 指针front, end 交叉后会outOfIndexException.
代码:
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0)
return true;
int front = 0;
int end = s.length() - 1;
while (front < end) {
if (!isValid(s.charAt(front)))
front++;
else if (!isValid(s.charAt(end)))
end--;
else {
if (isValidCase(s.charAt(front), s.charAt(end))){
front++;
end--;
} else {
return false;
}
}
}
return true;
}
public boolean isValid(char c) {
return Character.isLetter(c) || Character.isDigit(c);
}
public boolean isValidCase(char a, char b) {
if (a == b)
return true;
else if (Character.toUpperCase(a) == Character.toUpperCase(b))
return true;
else
return false;
}
}
代码优化:
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return true;
}
int front = 0;
int end = s.length() - 1;
while (front < end) {
while (front < s.length() && !isvalid(s.charAt(front))) { // nead to check range of a/b
front++;
}
if (front == s.length()) { // for empty string “.,,,”
return true;
}
while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
end--;
}
if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
return false;
} else {
front++;
end--;
}
}
return true;
}
private boolean isvalid (char c) {
return Character.isLetter(c) || Character.isDigit(c);
}
}
Lintcode415-Valid Palindrome-Medium的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- 25. Valid Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Valid Palindrome [LeetCode]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- OpenVPN 服务端(pritunl)的一些运维经验
1.当服务端部署在docker中,重启机器之后,docker服务会启动,pritunl的docker容器也会跟着自动启动.但此时,一些系统服务还未完全启动成功,导致会有一些pritunl server ...
- 【转】Kylin系列-使用Saiku+Kylin构建多维分析OLAP平台
关于Kylin的介绍和使用请参考之前的文章 <分布式大数据多维分析(OLAP)引擎Apache Kylin安装配置及使用示例> Kylin对外提供的是SQL查询接口,基于Kylin构建OL ...
- Vue + vant-UI 打造移动商城
- 2017-2018-2 20165215 实验四《Android开发基础》实验报告
2017-2018-2 20165215 实验四<Android开发基础>实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:张家佳 学号:20165215 指导教 ...
- qq网吧弹框如何去掉?如何删掉NetBar文件夹?
qq网吧弹框如何去掉?如何删掉NetBar文件夹?有些qq会弹出qq网吧,让人烦恼.而且点了那个不是网吧的反馈了多次都还会弹出.如何退出关闭删除取消去掉qq网吧呢,下面介绍一种解决方法:1.打开qq安 ...
- Python+OpenCV图像处理(十一)—— 图像金字塔
简介:图像金字塔是图像中多尺度表达的一种,最主要用于图像的分割,是一种以多分辨率来解释图像的有效但概念简单的结构.简单来说,图像金字塔就是用来进行图像缩放的. 进行图像缩放可以用图像金字塔,也可以使用 ...
- Java 注释规范
基本的要求: 1.注释形式统一 在整个应用程序中,使用具有一致的标点和结构的样式来构造注释.如果在其它项目中发现它们的注释规范与这份文档不同,按照这份规范写代码,不要试图在既成的规范系统中引入新的规范 ...
- 每日linux命令学习-read命令
read命令 作用 从标准输入中读取一行. 语法 read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p pro ...
- C++中static_cast和dynamic_cast强制类型转换
在C++标准中,提供了关于类型层次转换中的两个关键字static_cast和dynamic_cast. 一.static_cast关键字(编译时类型检查) 用法:static_cast < ty ...
- 给PXC集群加密
MySQL的复制时明文的,不管是集群的复制还是IST/SST,直接通过抓包就可以抓取数据. 生成证书 直接使用 mysql_ssl_rsa_setup mysql_ssl_rsa_setup --da ...