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. CGContextRef 用法总结

    0  CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文1 CGContextMoveToPoint 开始画线2 CGContext ...

  2. SDN测量论文粗读(一)9.19

    UMON: Flexible and Fine Grained Traffic Monitoring in Open vSwitch 论文来源:CoNext 发表时间:2015 解决问题及所做贡献:现 ...

  3. sharepoint rest api Add ListItem 报错

    Recently, I was trying to create a list item using Rest API on Sharepoint 2013. I got the following ...

  4. 浅淡 RxJS WebSocket

    const open$ = new Subject(); const ws = webSocket({ url: 'wss://echo.websocket.org', openObserver: o ...

  5. jQuery之修改li下样式和图片

    <script type="text/javascript"> $(document).ready(function(){ $('li').click(function ...

  6. Windows Redis 取消保护模式C#进行访问

    运行redis服务程序和客户端程序设置即可. config set protected-mode “no”

  7. laravel5.5源码笔记(二、服务提供者provider)

    laravel里所谓的provider服务提供者,其实是对某一类功能进行整合,与做一些使用前的初始化引导工作.laravel里的服务提供者也分为,系统核心服务提供者.与一般系统服务提供者.例如上一篇博 ...

  8. STM32F4XX中断方式通过IO模拟I2C总线Master模式

    STM32的I2C硬核为了规避NXP的知识产权,使得I2C用起来经常出问题,因此ST公司推出了CPAL库,CPAL库在中断方式工作下仅支持无子地址 的器件,无法做到中断方式完成读写大部分I2C器件.同 ...

  9. linux-2.6.22.6内核启动分析之配置

    配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...

  10. nexus3使用docker运行/创建docker私有仓库/maven私有仓库

    version: '3.2' services: nexus3: container_name: nexus3 hostname: nexus3 image: sonatype/nexus3:3.14 ...