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" 是一个回文. & ...
随机推荐
- 手动集成OWIN
1.Install-Package Microsoft.AspNet.Identity.Owin Owin的很大亮点之一就是它可以让我们的ASP.NET 网站摆脱IIS,但是毕竟大多数的ASP.NET ...
- 常用的SQL
--时间计算: select GETDATE() ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) --查 ...
- WiX and System Folders 系统目录 installshield 如何将文件安装到C盘根目录
Property name Brief description of property AdminToolsFolder Full path to the directory containing a ...
- Unity3D动画面板编辑器状态属性对照表
不推荐用AnimationUtility.SetEditorCurve问题很多,推荐AnimationCurve.AddKey.通过AnimationUtility.GetAllCurves可以获得编 ...
- redmine 的安装
https://bitnami.com/stack/redmine/installer#官方地址 安装很简单,给权限 chmod +x bitnami-redmine-3.3.0-1-linux-x6 ...
- 53. Reverse Words in a String【easy】
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- tornado部署
1.为什么要运行多个tornado实例同步请求时,在应用处理过程中(如数据库查询,磁盘访问),服务器进程不能接受新请求.所以需要运行多个服务器进程实例.异步请求时,在应用处理时,服务器进程是非阻塞的, ...
- swift 属性和方法
属性和常量 如果创建了一个结构体的实例并赋值给一个常量,则无法修改实例的任何属性: let rangeOfFourItems = FixedLengthRange(firstValue: 0, len ...
- [转]软件测试 Top 120 Blog (博客)
[转]软件测试 Top 120 Blog (博客) 2015-06-08 转自: 软件测试 Top 120 Blog (博客) # Site Author Memo DevelopSense M ...
- django 文件上传(阿里云oss)下载(支持大文件下载)
1.文件上传 Models 设计 class Upload_File(models.Model): image = models.FileField(upload_to='file/%Y/%m',de ...