415. Valid Palindrome【LintCode java】
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】的更多相关文章
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 389. Valid Sudoku【LintCode java】
Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where e ...
- 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 ...
- 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 ...
- leetcode:Valid Palindrome【Python版】
1.注意空字符串的处理: 2.注意是alphanumeric字符: 3.字符串添加字符直接用+就可以: class Solution: # @param s, a string # @return a ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
随机推荐
- [SCOI2005]互不侵犯(状压DP)
嗝~算是状压DP的经典题了~ #\(\mathcal{\color{red}{Description}}\) 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案.国王能攻 ...
- [转]SQL Server 数据库中的 MD5 和 SHA1
MD5 和 SHA1 是一种单向加密算法,常用于密码的验证等需要加密操作的场合,在一般情况下,开发人员可以通过 Delphi 或 PHP 这类语言自己编写相关函数或者使用自带的函数,然后将加密过的结果 ...
- 学习VCL之路(1)
在TObject类中,有一个Dispatch()方法和一个DefaultHandler()方法,它们都是与消息分发机制相关的. Dispatch()负责将特定的消息分发给合适的消息处理函数.首先它会在 ...
- 与MySQL的零距离接触
存储引擎 查看数据表的创建命令:show create table tbl_name
- version 1.5.2-04 of the jvm is not suitable for this product. version:1.6 or greater is required
这里仅仅说明一个可能造成该问题的解决办法,也是我遇到的原因. 这句话的意思是说,eclipse须要至少1.6版本号或以上的JVM ,而你仅仅有1.5.2版本号的JVM.想想就认为非常奇怪,我装的但是J ...
- iOS App占用太多磁盘空间
问题:随着App的不断运行,发现所占磁盘空间越来越大 分析:应该是网络下载中的缓存,包括利用SDWebImage产生的.和下载单个文件被取消后的缓存 验证:查看App目录中的Tmp(系统存放未下载完成 ...
- Ubuntu下VsCode+CMake 交叉编译
在安装配置好VsCode后,下载相关插件.如图: 其中CMake Tools是为了方便使用CMake的扩展工具. 在创建工程前,先在VSCode打开一个空的目录(你的Project目录),再对CMak ...
- L2-006 树的遍历 (后序中序求层序)
题目: 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行给出其后序遍历序 ...
- WebGl 旋转(矩阵变换)
代码1: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Delphi Firemonkey在主线程 异步调用函数(延迟调用)
先看下面的FMX.Layouts.pas中一段代码 procedure TCustomScrollBox.MouseDown(Button: TMouseButton; Shift: TShiftSt ...