Description

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

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.

Example

"A man, a plan, a canal: Panama" is a palindrome.

"race a car" is not a palindrome.

Challenge

O(n) time without extra memory.

解题:判断某字符串是否是回文字符串,其中标点可以不对称,但字母和数字必须要对称。可以通过Character中的函数, isLetterOrDigit( char ch )来判断。代码如下:

public class Solution {
/**
* @param s: A string
* @return: Whether the string is a valid palindrome
*/
public boolean isPalindrome(String s) {
// write your code here
s = s.toUpperCase();
for(int i = 0, j = s.length() - 1; i <= j; i++, j--){
if(s.charAt(i) == s.charAt(j))
continue;
else{
if(Character.isLetterOrDigit(s.charAt(i)) && Character.isLetterOrDigit(s.charAt(j)))
return false;
else{
if(Character.isLetterOrDigit(s.charAt(i))){
i--;
}else if(Character.isLetterOrDigit(s.charAt(j))){
j--;
}else{
continue;
}
}
}
}
return true;
}
}

415. Valid Palindrome【LintCode java】的更多相关文章

  1. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  2. 389. Valid Sudoku【LintCode java】

    Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where e ...

  3. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  4. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  5. leetcode:Valid Palindrome【Python版】

    1.注意空字符串的处理: 2.注意是alphanumeric字符: 3.字符串添加字符直接用+就可以: class Solution: # @param s, a string # @return a ...

  6. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  7. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  8. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  9. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

随机推荐

  1. Android Asynctask与Handler的比较,优缺点区别,Asynctask源码

    1  AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以 ...

  2. 浅析中国剩余定理(从CRT到EXCRT))

    前置知识 1. a%b=d,c%b=e, 则(a+c)%b=(d+e)%b(正确性在此不加证明) 2. a%b=1,则(d\(\times\)a)%b=d%b(正确性在此不加证明) 下面先看一道题(改 ...

  3. CentOS 7紧急救援模式修改root用户密码的方法

    最近无聊在网上搜索linux系统root用户密码破解方法,看来很多朋友的博文,同时也试了一下,但是感觉他们写的还是不是很清晰.简洁,因此自己就心血来潮写了这篇博文,提供一个比较清晰的思路给新手,如果有 ...

  4. K8s集群安装和检查(经验分享)

    一.组件方式检查 1. Master节点: root>> kubectl get cs 2. Node 节点: 无  二.服务方式检查 1. Master 节点: root>> ...

  5. linux 学习第十五天(vsftpd配置)

    一.vstapd配置 vsftpd 服务(a.匿名公开 b.系统本地账户验证c.虚拟专用用户验证) iptables -F  (清空防火墙) service iptables save  (保存防火墙 ...

  6. 树莓派ubuntu系统下修改config.txt文件 树莓派config.txt文件修改记录

    原文:https://www.raspberrypi.org/documentation/configuration/config-txt.md译文:http://my.oschina.net/fun ...

  7. XAMPP之Mysql启动失败

    启动XAMPP中的Mysql出现如下: 可能的原因是本地有多个MySQL,所以要在注册表编辑器中将imagePath改成XAMPP中的mysql的地址.(打开注册表编辑器:win+R,输入regedi ...

  8. 在vim编辑器中实现python的tab补全

    在vim编辑器中实现python的tab补全 在vim编辑器中实现python tab补全插件有Pydiction,Pydiction可以实现下面python代码的自动补全:   1.简单python ...

  9. package html to native application

    npm install nw -g npm . https://github.com/nwjs/nw.js/ https://github.com/nwjs/nw.js/wiki/How-to-run ...

  10. Hadoop命令大全

    Hadoop命令大全 分类: 云计算2011-03-01 15:04 6852人阅读 评论(0) 收藏 举报 hadoop作业任务集群class脚本 1.列出所有Hadoop Shell支持的命令   ...