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. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  2. ansible copy 模块 changed false 没有变化

    在分发配置文件的时候,我用命令ansible big_hosthub  -m copy -a "src=/home/clouder/deploy-conf.xml dest=/home/cl ...

  3. [iOS]CIDetector之CIDetectorTypeFace人脸识别

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  4. iOS笔记,开发经验总结【持续更新】

    1. 设置navigationBar 背景颜色有色差, 原因:如果单纯的设置背景颜色也是有高斯模糊处理的效果,对纯色高斯模糊处理过后相当于纯色的70%(猜测)透明化处理,但是反正就是有色差 解决方法一 ...

  5. HP-Socket国产优秀socket通信组件推荐

    来源:http://blog.csdn.net/clb929/article/details/51085983 * HP-Socket 官方网站:http://www.jessma.org * HP- ...

  6. 什么是ajax和json,说说他们的优缺点

    ajax异步传输的js和xml.实现页面无刷新状态更新页面和异步提交 所谓异步,简单解释就是:向服务器发送请求的时候,我们不必等待结果,而是同时做其他的事情,等到有了结果后它自己会根据设定进行后续操作 ...

  7. php (zip)文件下载设置

    普通下载头大概意思,文件输出的地方二选一,小文件下载.如文件较大时注意执行时间与内存使用.可以看php大文件下载 $filename = $_GET['filename']; $pathname = ...

  8. BurpSuite系列(十)----Extender模块(扩展器)

    一.简介 Burp在软件中提供了支持第三方拓展插件的功能,方便使用者编写自己的自定义插件或从插件商店中安装拓展插件.Burp扩展程序可以以多种方式支持自定义Burp的行为,例如:修改HTTP请求和响应 ...

  9. ACM1009:FatMouse' Trade

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  10. 【洛谷】 3264 [JLOI2015] 管道连接

    前言:     如果还不知道斯坦纳树的童鞋可以看这两篇博客: 我的:https://blog.csdn.net/jerry_wang119/article/details/80001711 我一开始学 ...