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可以定义两个指针。

注意:

  1. 两个指针命名,front 和 end,不要用i, j
  2. 同类中:方法与方法之间直接用方法名来互相调用。Java方法的调用。
  3. 多个if并列 和 else if 区别:If you have used multiple if statements then if the condition is true all will be executed. If you have used if and else if combination only one will be executed where first comes the true value 
    所以,如果把line 10 12 14 变成并列if,output会出错。因为如果连续出现两个或以上的非字母数字字符的话,就会输出false。比如 String s = a'+ba;
  4. boolean方法命名以is开头。
  5. 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的更多相关文章

  1. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  3. Leetcode Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  6. 25. Valid Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  7. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. Valid Palindrome [LeetCode]

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

  10. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. Linux基础命令---杀死进程pkill

    pkill pkill可以给指定的进程发送信息,它可以结束某个执行的进程或者目录登录的用户. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...

  2. 区块链区块的生成和链接,比特币btc的产生,UTXO的生成和消耗,比特币系统

    区块链区块的生成和链接,比特币btc的产生,UTXO的生成和消耗,比特币系统 区块链区块的生成和链接,比特币btc的产生,UTXO的生成和消耗,比特币系统

  3. localstorage跨域解决方案

    localstorage也存在 跨域的问题, [解决思路如下] 在A域和B域下引入C域,所有的读写都由C域来完成,本地数据存在C域下; 因此 A哉和B域的页面必定要引入C域的页面; 当然C域最好是主域 ...

  4. webpack系统配置

    简言之,webpack 是一个模块打包器 (module bundler),能够将任何资源如 JavaScript 文件.CSS 文件.图片等打包成一个或少数文件. 为什么要用Webpack? 首先, ...

  5. Poj3253 Fence Repair (优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 67319   Accepted: 22142 De ...

  6. HDU 1233 还是畅通工程 (最小生成树 )

    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...

  7. hdu 1466 计算直线的交点数 递推

    题目描述 平面上有n条直线,且无三线共点,问这些直线能有多少种不同交点数. 比如,如果n=2,则可能的交点数量为0(平行)或者1(不平行). 输入 输入数据包含多个测试实例,每个测试实例占一行,每行包 ...

  8. Ubuntu 为 root 帐号开启 SSH 登录

    1. 修改 root 密码sudo passwd root 2. 以其他账户登录,通过 sudo nano 修改 /etc/ssh/sshd_config :xxx@ubuntu:~$ su - ro ...

  9. 图片转化成base64字符串

    package demo; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; public ...

  10. 关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题

    当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示: <!-- Spring字符编码过滤器:解决中文乱码问题 --> < ...