首页 > 代码库 > valid-palindrome——判断带符号数字字母的字符串是否为回文
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.
字符串为空时判断为回文,大小写不区分可确定相等,数字与字母不同。
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——判断带符号数字字母的字符串是否为回文
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。