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" 是一个回文. & ...
随机推荐
- ASP.NET配置文件machine.config与性能[转]
转 http://www.cnblogs.com/chenlulouis/archive/2010/05/26/1744261.html http://www.cnblogs.com/zhangron ...
- Android 中 字符串比较
EditText中getText().toString() 得到的字符串 写法如下if(m_txtAddress.getText().toString()=="") 这样写 是不会 ...
- php 检查该数组有重复值
if (count($array) != count(array_unique($array))) { echo '该数组有重复值'; }
- ajax application/json 的坑
我们习惯使用application/json方式提交,所以会在ajax中指定contentType. $.ajax({ url: "http://localhost:3000", ...
- unity, destroy gameObject & destroy all children
一,destroy gameObject 删除名为xxx的gameObject 错误方法1: Destroy(xxx); 以上方法之所以错误,是因为Destroy在下一帧才生效,而在本帧之内xxx还存 ...
- Mysql User表权限字段说明全介绍
一:mysql权限表user字段详解: Select_priv.确定用户是否可以通过SELECT命令选择数据. Insert_priv.确定用户是否可以通过INSERT命令插入数据. Update_p ...
- MySQL是如何做到安全登陆
首先Mysql的密码权限存储在mysql.user表中.我们不关注鉴权的部分,我们只关心身份认证,识别身份,后面的权限控制是很简单的事情.在mysql.user表中有个authentication_s ...
- Ubuntu下单网卡多IP地址的配置
删除用户默认配置文件 由于在默认清空下,配置文件是系统自动生成的用户设备配置文件保存在/etc/udev/rule.d/下面:# cp /etc/udev/rule.d /etc/udev/rule. ...
- Spring Oauth2 with JWT Sample
https://www.javacodegeeks.com/2016/04/spring-oauth2-jwt-sample.html ******************************** ...
- ibatis时间比较大小
<![CDATA[ A.RFID_Time >= #StartTime# ]]>时间搜索功能 A.RFID_Time <![CDATA[ ...