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

valid-palindrome——判断带符号数字字母的字符串是否为回文

时间:2017-06-13 14:39:56      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:pre   字符   for   isp   alpha   als   lan   log   tsig   

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 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int n=s.length();
 5         if(n==0) return true;
 6         int i=0,j=n-1;
 7         while(i<j){
 8             if(!isCharacters(s[i])){
 9                 i++;
10                 continue;
11             }
12             if(!isCharacters(s[j])){
13                 j--;
14                 continue;
15             }
16                 
17             int left=0,right=0,leftsig=0,rightsig=0;
18             left=(s[i]>=0&&s[i]<=9)?s[i]-0:((s[i]>=a&&s[i]<=z)?s[i]-a:s[i]-A);
19             right=(s[j]>=0&&s[j]<=9)?s[j]-0:((s[j]>=a&&s[j]<=z)?s[j]-a:s[j]-A);
20             leftsig=(s[i]>=0&&s[i]<=9)?0:1;
21             rightsig=(s[j]>=0&&s[j]<=9)?0:1;
22             if(left!=right||leftsig!=rightsig)
23                 return false;
24             i++;
25             j--;
26         }
27         return true;
28     }
29     bool isCharacters(char c){
30         if((c>=a&&c<=z)||(c>=A&&c<=Z)||(c>=0&&c<=9))
31             return true;
32         else
33             return false;
34     }
35 };

 

valid-palindrome——判断带符号数字字母的字符串是否为回文

标签:pre   字符   for   isp   alpha   als   lan   log   tsig   

原文地址:http://www.cnblogs.com/zl1991/p/7000512.html

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