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" 是一个回文. & ...
随机推荐
- <a>标签实现锚点跳跃,<a>标签实现href不跳跃另外加事件(ref传参)
1.锚点跳跃 HTML: <div class="page_title" id="maodian"> <h1>客房节日价格管理</ ...
- SQL 防止注入
var strsql = "insert into Staff_Answer (ExamTitleID,QuestionsID,MultipleChoice,RightOption,Answ ...
- 阿里云RDS实例内不同数据库之间的数据迁移
适用场景 本文适用于使用DTS实现相同实例下库名不同的数据库之间的数据迁移.本文以使用DTS将同一RDS实例下的amptest库迁移到jiangliu_amptest库为例来说明如何使用DTS实现相同 ...
- 编码gbk ajax的提交
客户端
- 深入理解MapReduce的架构及原理
1. MapReduce 定义 Hadoop 中的 MapReduce是一个使用简单的软件框架.基于它写出来的应用程序能够执行在由上千个商用机器组成的大型集群上,并以一种可靠容错式并行处理TB级别的数 ...
- XMLDocument转为String 摘录
public static string FormatXmlString(string xmlString) { XmlDocument document = new XmlDocument(); d ...
- hibernate中一对一映射
一.hibernate中一对一映射有两种 1 主键方式,一张表的主键是通过另一张表的主键生成的 2 外键方式,一张表添加外键引用另一张表的主键,并添加唯一unique约束 二.下面进行简单例子,用户和 ...
- GameObject非激活状态,触发测试
测试结果为OnEnable,Start不执行.Awake不管激活未激活都执行 不管是驻留在层级面板,还是手动拖上去. 但是,在任何时间设为激活.就会2个都触发.Awake和Start只触发一次.这个问 ...
- [k8s]metricbeat的kubernetes模块&kube-metric模块
正确姿势启动metricbeat metricbeat.modules: - module: system metricsets: - cpu - filesystem - memory - netw ...
- JAVA多线程之synchronized和volatile实例讲解
在多线程中,提到线程安全.线程同步,我们经常会想到两个关键字:volatile和synchronized,那么这两者有什么区别呢? 1. volatile修饰的变量具有可见性 volatile是变量修 ...