LeetCode 125 Valid Palindrome(有效回文)(*)
翻译
给定一个字符串。确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它。
比如。
“A man, a plan, a canal: Panama” 是回文的。
“race a car” 不是回文的
批注:
你是否考虑了字符串可能为空?这样的面试的时候是一个好问题。
对于这问题的意图,我们定义空字符串是回文的。
原文
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.
分析
题目解出来并非非常难。只是须要细心只是还是会出错。
第一步就是要从原字符串中的干扰去掉,也就是仅仅保留字母。这能够通过ascii来推断。
然后构造出新的字符串。再推断这个新字符串是否是回文的就好了。
然而我也还是错了。首先我没想清晰题目的样例。”aA”也是回文的。
再一次改动之后我还是错了。由于题目的alphanumeric是字母数字均包括了,我一開始翻译的就是字母,后来上文的翻译也改动了。
于是我确定彻底的改革,将功能独立出来,然而代码好长了。可是非常清晰:
代码
class Solution {
public:
bool isAlpha(char c) {
int ascii = (int)c;
if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122))
return true;
else return false;
}
bool isNumber(char c) {
int ascii = (int)c;
if (ascii >= 48 && ascii <= 57)
return true;
else return false;
}
bool isAlphaAndNumber(char c1, char c2) {
if ((isAlpha(c1) && isNumber(c2)) || (isAlpha(c2) && isNumber(c1)))
return true;
else return false;
}
bool isPalindrome(string s) {
if (s.size() == 0) return true;
string newStr = "";
for (int i = 0; i < s.size(); ++i) {
// 仅仅取出当中的字母和数字
if (isAlpha(s[i]) || isNumber(s[i]))
newStr += s[i];
}
for (int i = 0; i < newStr.size() / 2; ++i) {
// 两者一个是字母、一个是数字
if (isAlphaAndNumber(newStr[i], newStr[newStr.size() - i - 1]))
return false;
// 两者均为数字
else if (isNumber(newStr[i]) && isNumber(newStr[newStr.size() - i - 1])) {
// 推断是否是同一个数字
if (newStr[i] != newStr[newStr.size() - i - 1])
return false;
}
// 两者均为字母
else {
// 前面推断是否是同一个字母,后面推断是否是互为大写和小写
if (newStr[i] != newStr[newStr.size() - i - 1] && abs((int)newStr[i] - (int)newStr[newStr.size() - i - 1]) != 32)
return false;
}
}
return true;
}
};
进阶
一想到ascii就忘了还有toupper这些函数了,别人写的……
class Solution {
public:
bool isPalindrome(string s) {
int l = 0, r = s.size() - 1;
while(l <= r){
while(!isalnum(s[l]) && l < r) l++;
while(!isalnum(s[r]) && l < r) r--;
if(toupper(s[l]) != toupper(s[r])) return false;
l++, r--;
}
return true;
}
};
LeetCode 125 Valid Palindrome(有效回文)(*)的更多相关文章
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 125 Valid Palindrome 验证回文字符串
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode 125. Valid Palindrome ----- java
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
随机推荐
- Cordova 快速入门记录
本篇文章由:http://xinpure.com/cordova-quick-start-recording/ 记一笔 Cordova 官网入门文档 Get Started Fast,言简意该.通俗易 ...
- Solr 搜索功能使用
http://wiki.apache.org/solr/SolrQuerySyntax http://www.solrcn.com/index.php?s=查询
- Python2.X和Python3.X中的urllib区别
Urllib是Python提供的一个用于操作URL的模块,在Python2.X中,有Urllib库,也有Urllib2库,在Python3.X中Urllib2合并到了Urllib中,我们爬取网页的时候 ...
- 使用VS2012遇到的问题
问题1:VS2012 编译程序时:无法查找或打开PDB文件 解决方法:调试-选项-符号-Microsoft符号服务器打钩,然后确定,就OK了. 问题2:按F5运行.c程序,dos窗口闪退 解决方法:C ...
- [原]MS SQL表字段自增相关的脚本
--查询表是否有自增字段 SELECT OBJECTPROPERTY(OBJECT_ID('[表名]'), 'TableHasIdentity') --查询表的自增字段 SELECT COLUMN_N ...
- 百度地图 驾车/公交查询Demo(支持多起点多终点)
效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- 字符串函数---itoa()函数具体解释及实现
itoa()函数 itoa():char *itoa( int value, char *string,int radix); 原型说明: value:欲转换的数据. string:目标字符串的地址. ...
- Unity3D 使用XML进行简单的配置文件改动
1.首先是看看效果图: 開始执行项目例如以下图所看到的 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2NsdW9qaWpp/font/5a6L5L2T/ ...
- [svc]jq神器使用
jq神器 处理json数据 支持过滤某字段 支持数学运算(对字段处理) 安装 yum install -y jq 使用 参考: http://blog.just4fun.site/command-to ...
- OGNL表达式的基本语法和用法
首先我们一起来看一下OGNL中的#.%和$符号. 关于OGNL各种用法总结参看:http://blog.163.com/seara520@126/blog/static/720693042010320 ...