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" 是一个回文. & ...
随机推荐
- Linux下的进程间通信-详解
详细的讲述进程间通信在这里绝对是不可能的事情,而且笔者很难有信心说自己对这一部分内容的认识达到了什么样的地步,所以在这一节的开头首先向大家推荐著 名作者Richard Stevens的著名作品:&l ...
- javascript通用函数库
/* -------------- 函数检索 -------------- trim函数: trim() lTrim() rTrim() 校验字符串是否 ...
- RESTful Web Service实战 小结1
1 REST的基本实现形式HTTP+URI+XML,但不是唯一形式.XML后来被Json格式替代.REST是一中架构风格(Representational State Transfer,表述性状态转移 ...
- jdbc链接基础
1 jdbc 链接两种方式,通过jdbc链接mysql数据库,url:jdbc:mysql://ip:端口[/database name] 通过什么驱动器,链接什么数据库,数据库的ip,连接端口,可以 ...
- C#趣味程序---爱因斯坦的台阶问题
问题:设有一阶梯,每步跨2阶.最后余1阶.每步跨3阶.最后余2阶:每步跨5阶.最后余4阶:每步跨6阶.最后余5阶:每步跨7阶.刚好到阶顶.问共同拥有多少阶梯? using System; namesp ...
- Phonegap创建项目语法
1:在本地磁盘新建一个文件夹存放要开发的项目,比如在D盘建一个yun文件夹2 2:然后在dos系统下,d:命令进入d盘,cd yun进入yun文件夹, 3:然后phonegap create yun2 ...
- struts2异常处理机制
一.处理一般异常(javaBean异常) struts2进行异常处理首先需要添加exception拦截器,而默认拦截器栈已经加入了这个拦截器,所以不用特意的声明.在Struts 2框架中,采用声明式异 ...
- js delete
在开始之前,先让我们看一段代码 >>> var sum = function(a, b) {return a + b;} >>> var add = sum; &g ...
- 数据库填充DataSet,逐行访问
DataSet 对象是 Microsoft .NET 框架中数据访问的关键部分,是可保存表.视图和关系的内存中对象.本文介绍如何使用一个或多个数据库查询的结果填充 DataSet 对象,以及在将这些数 ...
- hive 和Hbase的pom文件
<hadoop-common></hadoop-common> <hadoop-hdfs></hadoop-hdfs> <dependency&g ...