码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode】Valid Palindrome

时间:2016-06-06 01:09:37      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:https://leetcode.com/problems/valid-palindrome/

题目:

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

For 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.

算法

[java] view plain copy
 技术分享技术分享
  1. public boolean isDecent(char c) {  
  2.         if (Character.isAlphabetic(c) || Character.isDigit(c)) {  
  3.             return true;  
  4.         } else  
  5.             return false;  
  6.     }  
  7.   
  8.     public boolean isPalindrome(String s) {  
  9.         char c[] = s.toLowerCase().toCharArray();  
  10.         int i = 0, j = c.length - 1;  
  11.         while (i < j) {  
  12.             if (isDecent(c[i]) && isDecent(c[j])) {  
  13.                 if (c[i] == c[j]) {  
  14.                     i++;  
  15.                     j--;  
  16.                 } else {  
  17.                     return false;  
  18.                 }  
  19.             }  
  20.             if (!isDecent(c[i])) {  
  21.                 i++;  
  22.             }  
  23.             if (!isDecent(c[j])) {  
  24.                 j--;  
  25.             }  
  26.         }  
  27.         return true;  
  28.     }  


【Leetcode】Valid Palindrome

标签:

原文地址:http://blog.csdn.net/yeqiuzs/article/details/51590922

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!