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 ...
随机推荐
- tensorflow学习3---mnist
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data '''数据下载''' mnist= ...
- HDU 2176 取(m堆)石子游戏 (尼姆博奕)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 m堆石子,两人轮流取.只能在1堆中取.取完者胜.先取者负输出No.先取者胜输出Yes,然后输出怎 ...
- AtCoder Beginner Contest 044 B - 美しい文字列 / Beautiful Strings
Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement Let w be a string cons ...
- SQL表分区之二
前面说的给表做表分区,现在有个问题,比如上面我们说的是按照20w为一个分割线,那些现在我们想把这个调整下怎么办?难道要把之前的分区函数和分区方案删了,重新新建分区函数和分区方案嘛? 当然,此方式肯定是 ...
- python-数据分析与展示(Numpy、matplotlib、pandas)---2
笔记内容整理自mooc上北京理工大学嵩天老师python系列课程数据分析与展示,本人小白一枚,如有不对,多加指正 1.python自带的图像库PIL 1.1常用API Image.open() ...
- 合并ts到mp4
这个比较好用. copy /b d:\xxx\download_ts\* d:\xxx\download_ts\new.mp4 用python ffmpeg也可以,不过我合出来有卡顿或者掉声问题, ...
- mysql 通过查看mysql 配置参数、状态来优化你的mysql
我把MYISAM改成了INNODB,数据库对CPU方面的占用变小很多' mysql的监控方法大致分为两类: 1.连接到mysql数据库内部,使用show status,show variables,f ...
- Golang字符串函数认识(一)
package main import ( "fmt" "strings" "strconv" ) func main(){ //返回字符串 ...
- TCP/IP编程——基于TCP的半关闭
在TCP服务端和客户端建立连接之后服务端和客户端会分别有两个独立的输入流和输出流,而且相互对应.服务端的输出流对应于客户端的输入流,服务端的输入流对应于客户端的输出流.这是在建立连接之后的状态. 当我 ...
- VS 域名绑定IIs 调试
一排致敬老师傅:https://www.cnblogs.com/woxpp/p/5805624.html 问题: 域名www.jinhuang.com,网站的ip地域跳转出问题,需要测试. ①修改Ho ...