首页 > 其他 > 详细

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

时间:2017-06-13 13:46:48      阅读:288      评论:0      收藏:0      [点我收藏+]

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——判断带符号数字字母的字符串是否为回文

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

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