leetcode题解:Valid Palindrome(判断回文)
题目:
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.
说明:
1)注意两点:a、判断满足条件的字符 b、大写变小写(本题认为不区分大小写)
2)字符串为空则true
实现:
class Solution {
public:
bool isPalindrome(string s) {
if(s.empty()) return true;
int len=strlen(s.c_str());
int i=;
int j=;
for (;i<len;i++)
{
if (isChar(s[i]))//去其他符合,若有大写则大写变小写
{
s[i] = tolower(s[i]);//库函数
s[j++]=s[i];
}
}
s[j]='\0';
if (s[]=='\0') return true;
int len2=strlen(s.c_str());
i=;
while(i<=len2--i) //判断是否回文
{
if(s[i]!=s[len2--i]) return false;
i++;
}
return true;
}
private:
bool isChar(char t)//判断是否满足条件字符
{
if (('a' <= t&&t<='z')||('A' <= t&&t<='Z')||('' <= t&&t<=''))
{
return true;
}
else
return false;
}
};
leetcode题解: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 ...
- [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】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- [Leetcode] valid palindrome 验证回文
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" 是一个回文. & ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- code forces 990C
http://codeforces.com/contest/990/problem/C C. Bracket Sequences Concatenation Problem time limit pe ...
- transition transform animate的使用
注:代码显示效果可以自行粘贴复制查看 transition(过渡),主要是关注property的变化主要有四个属性transition-property.transition-durantion.tr ...
- js中字符串常规操作
string对象属性: 1.length 获取字符串的长度,需要注意的是,js中中文每个汉字也只代表一个字符. var myName="xulinjun"; console.log ...
- Error C1189: #error: Please use the /MD switch for _AFXDLL builds(转)
原文转自 https://www.cnblogs.com/zwh0214/p/6048360.html 在VS 2013中编译程序时出现错误: 错误提示1: error C1189: #error : ...
- Linux中brk()系统调用,sbrk(),mmap(),malloc(),calloc()的异同【转】
转自:http://blog.csdn.net/kobbee9/article/details/7397010 brk和sbrk主要的工作是实现虚拟内存到内存的映射.在GNUC中,内存分配是这样的: ...
- Daemon Process
Daemon Process 守护进程(Daemon)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待 处理某些发生的事件.守护进程是一种很有用的进程. Lin ...
- Oracle sql例子
create database oracle1 )) select * from class ,'一班') ,'二班') ,'三班') --创建学生表 ),cid int ,constraint FK ...
- C#中一些关键字和罕见类
1.@ 在处理字符串时,那些个字符串转义字符我们可以使用这个,那@就是为解决这个而诞生的,直接让字符串原样输出有木有?什么sql字符串了,还有路径啥了的,统统搞定 string sql = @&quo ...
- JavaScript基础之原型对象和原型链
原型对象 原型对象简单来说就是函数的原型所指向的对象.前面说原型的时候,说了Object.prototype所指对象就是Object(函数)的原型对象.在每个函数的原型对象中,默认会有construc ...
- 洛谷——P1258 小车问题
P1258 小车问题 题目描述 甲.乙两人同时从A地出发要尽快同时赶到B地.出发时A地有一辆小车,可是这辆小车除了驾驶员外只能带一人.已知甲.乙两人的步行速度一样,且小于车的速度.问:怎样利用小车才能 ...