首页 > 其他 > 详细

Valid Palindrome(LintCode)

时间:2015-12-06 17:26:56      阅读:147      评论:0      收藏:0      [点我收藏+]

Valid Palindrome

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

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

"race a car" is not a palindrome.

Note

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.

Challenge

O(n) time without extra memory.

 

深刻体会到了英文的用处。。。题目的中文翻译真是误导人。

技术分享
 1 public class Solution {
 2     /**
 3      * @param s A string
 4      * @return Whether the string is a valid palindrome
 5      */
 6     public boolean isPalindrome(String s) {
 7         char[] cs = s.toCharArray();
 8         int i = 0;
 9         int j = cs.length - 1;
10         
11         while(i<j) {
12             while(i<j && !Character.isDigit(cs[i]) && !Character.isLetter(cs[i])) i++;
13             while(i<j && !Character.isDigit(cs[j]) && !Character.isLetter(cs[j])) j--;
14             if(i >= j) break;
15             String ss = cs[i] + "";
16             String ss1 = cs[j] + "";
17             if(!ss.equalsIgnoreCase(ss1)) 
18                 return false;
19                 
20             i++;
21             j--;
22         }
23         return true;
24     }
25 }
View Code

 

Valid Palindrome(LintCode)

原文:http://www.cnblogs.com/FJH1994/p/5023763.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!