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. Future复习笔记

    1. Future就是对于具体的Runnable或者Callable任务的执行结果进行取消.查询是否完成.获取结果.必要时可以通过get方法获取执行结果,该方法会阻塞直到任务返回结果. Future类 ...

  2. java实现 HTTP/HTTPS请求绕过证书检测代码实现

    java实现 HTTP/HTTPS请求绕过证书检测代码实现 1.开发需求 需要实现在服务端发起HTTP/HTTPS请求,访问其他程序资源. 2.URLConnection和HTTPClient的比较 ...

  3. VPS采用的几种常见技术(OVZ、Xen、KVM)介绍与对比

    很多人看到同样配置的VPS价格相差很大,甚是不理解,其实VPS使用的虚拟技术种类有很多,如OpenVZ.Xen.KVM.Xen和HVM与PV. 在+XEN中pv是半虚拟化,hvm是全虚拟化,pv只能用 ...

  4. 75.Java异常处理机制-自定义异常

    package testDate; //自定义异常 public class MyException extends Exception{ public MyException(){ } public ...

  5. Tomcat启动报错:[Failed to start component]的解决方案

    在MyEclipse中启动Tomcat,该Tomcat仅部署了一个报错项目,启动Tomcat Server的全部信息如下: usage: java org.apache.catalina.startu ...

  6. 计算概论(A)/基础编程练习1(8题)/2:苹果和虫子

    #include<stdio.h> #include<math.h> int main() { /* n个苹果 每x小时能吃掉一个苹果 经过y小时 */ float n, x, ...

  7. python基础:re模块匹配时贪婪和非贪婪模式

    python贪婪和非贪婪 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符:非贪婪则相反,总是尝试匹配尽可能少 ...

  8. Angular 快速入门

    Angular 快速入门 AngularJS 官方网址 Angular:https://www.angular.cn/ Angular官网:https://angularjs.org/ Angular ...

  9. 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢

    目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...

  10. 【python005-数据类型】

    数据类型 一.字符串的相加是拼接,数字的相加是求和 二.python的数值类型:整形,浮点型,e记法,布尔类型 >>> 1.2e412000.0>>> 1.2e-4 ...