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. storm的trident编程模型

    storm的基本概念别人总结的, https://blog.csdn.net/pickinfo/article/details/50488226 编程模型最关键最难就是实现局部聚合的业务逻辑聚合类实现 ...

  2. Django框架----Object Relational Mapping(ORM)

    Django中的ORM Django项目使用MySQL数据库 1. 在Django项目的settings.py文件中,配置数据库连接信息: DATABASES = { "default&qu ...

  3. 在centos上搭建Git服务器

    第一步:先安装一些相关依赖库和编译工具 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel yum in ...

  4. glog日志库移植Android平台

    1.在linux平台下使用ndk交叉编译链编译glog生成libglog.a静态库. 2.将生成的库文件与头文件放到Android项目中,使用JNI方法调用. 3.编译遇到错误“stderr.stdo ...

  5. ubuntu 18.04下安装Java

    参照以下链接,这个是我找到的最易上手的学习教程了 https://blog.csdn.net/sangewuxie/article/details/80958611 按其步骤,我下载的是Java 11 ...

  6. Tensorflow学习笔记03-使用神经网络做线性回归

    import tensorflow as tf import numpy as np #input就是输入数据,输入矩阵,in_size就是输入矩阵的列数(数据属性数量),out_size输出矩阵列数 ...

  7. Python3 Pandas的DataFrame数据的增、删、改、查

    Python3 Pandas的DataFrame数据的增.删.改.查 一.DataFrame数据准备 增.删.改.查的方法有很多很多种,这里只展示出常用的几种. 参数inplace默认为False,只 ...

  8. Caused by: com.rabbitmq.client.ShutdownSignalException: connection error

    周五下午的时候升级了一个环境,跑了批处理sh升级脚本后,启动时报下列错误: INFO | jvm 1 | 2017/02/24 17:39:09 | java.io.IOException INFO ...

  9. 基于ZooKeeper和Thrift构建动态RPC调用

    一.基本功能 实现服务端向ZooKeeper集群注册自己提供的服务,并且把自己的IP地址和服务端口创建到具体的服务目录下.客户端向ZooKeeper集群监听自己关注的RPC服务(例如:sayHello ...

  10. 20145320《网络对抗》注入Shellcode并执行

    20145320注入Shellcode并执行 准备一段Shellcode 首先先准备一段C语言代码:这段代码其实和我们的shell功能基本一样 为了之后能够看到反汇编的结果,这次采用的静态编译.正常返 ...