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. nRF5 SDK for Mesh(五) Light switch demo 点灯例子

    Light switch demo  灯开demo   Purpose This demo project consists of four sub examples - The light swit ...

  2. 使用XWAF框架(4)——LunarCalendar日历组件

    XWAF提供了管理日历的com.xwaf.date.LunarCalendar静态类,可以直接使用,非常方便.该类包括六个主要静态方法: 4.1  isLeapYear(int year) 判断公历年 ...

  3. Spring Boot与Mybatis 借助Fastjson快速完成数据解析入库

    通过Spring Boot可以快速搭建一个项目结构,在此基础上本文就通过一个简单的例子,说明如何结合Mybatis 和 Fastjson,快速的完成一个数据的入库基本操作. 添加相关的依赖 <d ...

  4. Notes 20180505 : 计算机的基础知识

    总是想要去深入了解一下计算机,可真正去了解的时候才发现那并非一日之功,关于计算机的学习,并未放弃,但是化知识为笔记尚需时日,今日我们先简单了解一下计算机,然后开始Java语言的学习. 1 计算机的基础 ...

  5. centos7字体中英文转化

    [root@localhost oracle]#vi /etc/locate.conf,把里面的内容改为: 转化为英文: LANG="en_US.UTF-8"LANGUAGE=&q ...

  6. 【Python】01 - 常见用法随见随梳理

    1. range() 和 xrange()的区别 for x in range(5): print x for x in xrange(5): print x 这么看,range和xrange返回的值 ...

  7. Java常用修饰符总结

    修饰符是用于限定类型以及类型成员申明的一种符号,可用于修饰类.变量和方法,分为访问修饰符和非访问修饰符.访问修饰符控制访问权限,不同的访问修饰符有不同的权限范围,而非访问修饰符则是提供一些特有功能. ...

  8. 利用Git Bash 远程访问服务器

    首先 先在自己的当前主机打开git bash ssh-keygen 生成密钥对 (默认就好,我自己是一直摁着回车的) cat ~/.ssh/id_rsa.pub 查看生成好的公钥,并复制好 打开你远端 ...

  9. 虚拟机重启网络服务失败,当查看状态显示错误Failed to start LSB......

    重启网络失败截图 从本质上来看出现这样的问题,是因为拷贝过来的虚拟机重新分配了网卡MAC地址.这样造成的结果是配置文件中MAC与当前网卡MAC不一致.所以只需要修改一下配置文件即可. 用ip addr ...

  10. MySQL----MySQL数据库入门----第四章 单表查询

    select [distinct] * | 字段1,字段2,字段3... from 表名 [where 条件表达式] [group by 字段名] [having 条件表示式] [order by 字 ...