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

leetcode题解:Valid Palindrome(判断回文)

时间:2014-07-09 21:38:15      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   os   for   

题目:

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.

说明:

       1)注意两点:a、判断满足条件的字符  b、大写变小写(本题认为不区分大小写)

       2)字符串为空则true

实现:

 1 class Solution {
 2 public:
 3         bool isPalindrome(string s) {
 4             if(s.empty()) return true;
 5             int len=strlen(s.c_str());
 6             int i=0;
 7             int j=0;
 8             for (;i<len;i++)
 9             {
10                 if (isChar(s[i]))//去其他符合,若有大写则大写变小写
11                 {
12                     s[i] = tolower(s[i]);//库函数
13                     s[j++]=s[i];
14                 }
15             }
16             s[j]=\0;
17             if (s[0]==\0) return true;
18             int len2=strlen(s.c_str());
19             i=0;
20             while(i<=len2-1-i) //判断是否回文
21             {
22                 if(s[i]!=s[len2-1-i]) return false;
23                 i++;
24             }
25             return true;            
26     }
27     private:
28         bool isChar(char t)//判断是否满足条件字符
29         {
30             if ((a <= t&&t<=z)||(A <= t&&t<=Z)||(0 <= t&&t<=9))
31            {
32                return true;
33            }
34            else
35                return false;           
36         }
37 };

 

 

leetcode题解:Valid Palindrome(判断回文),布布扣,bubuko.com

leetcode题解:Valid Palindrome(判断回文)

标签:style   blog   color   strong   os   for   

原文地址:http://www.cnblogs.com/zhoutaotao/p/3829994.html

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