【LeetCode练习题】Valid Palindrome
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.
判断是否是回文字符串。
解题思路:
刚拿到这个题啊,因为之前 递归反转栈 那道题的影响,我直接就上了递归了。即判断是否是回文字符串,先判断第一个和最后一个字符是否相同,如果相同,则取中间的串为子串进行递归。
递归返回条件是字符串只有一个字符或两个字符的时候。
于是我写了以下的代码:
class Solution {
bool isPalin(string s) {
if(s.size() == )
return true;
if(s.size() == )
if(s[] == s[])
return true;
else
return false;
if(s[] == s.back()){
string sub = s.substr(,s.size()-);
bool isSub = isPalindrome(sub);
if(isSub)
return true;
}
return false;
}
public:
bool isPalindrome(string s) {
if(s.size() == )
return true;
string str2;
for(int i = ;i < s.size();i++){
if(isalnum(s[i]))
str2.append(,tolower(s[i]));
}
if(str2.size() == )
return true;
return isPalin(str2);
}
};
提交上去以后……
超时!!
果然还是我想多了。。。
其实这一题特别简单,一个指针指向第一个,一个指针指向最后一个,遍历一遍就可以了。遇到非数字字母字符的忽略掉,如果两个字符不相等,就return false。
代码如下:
class Solution {
public:
bool isAlphanumeric(char &c){
if(c >= 'A' && c <= 'Z'){
c = c | 0x20;
return true;
}
else if( c >= '' && c <= '' || c >= 'a' && c <= 'z')
return true;
else
return false;
}
bool isPalindrome(string s) {
int i = ;
int j = s.size() - ;
while(i < j){
if(!isAlphanumeric(s[i]))
++i;
else if(!isAlphanumeric(s[j]))
--j;
else if(s[i++] != s[j--])
return false;
}
return true;
}
};
【LeetCode练习题】Valid Palindrome的更多相关文章
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode] 1. Valid Palindrome
leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...
- LeetCode 1216. Valid Palindrome III
原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...
- [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 ...
- 【题解】【字符串】【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 ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- leetcode:Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- C++基础回顾2(函数, 指针和引用)
接着回顾函数.指针和应用. 函数 1.多维数组作为形参时,第一维的大小可以省略(也可以不省略),但是其他维的大小必须指定.比如二维数组形参,int array[3][]不正确,int arry[][1 ...
- NSDate显示和时区的关系
在网上看到一篇介绍NSDate的博文.在它的“NSDate初始化“章节,说在使用 NSLog(@"当前时间 date = %@",[NSDate date]);时,显示出来的时间 ...
- PHP常用魔术方法(__call魔术方法:)
魔术方法 __call <?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload ...
- CGI、FastCGI和php-fpm的区别
参考网上的描述,将网上的描述内容,整理进来: 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.web server(比如说nginx)只是 ...
- UGUI 下拉滚动框
开始制作好友系统了, 发现有一个UI跟QQ的面板一模一样. 于是就写了一个公共的下拉滚动框.需要把按钮的中心点(pivot.y = 1),描点为最上方 直接上图吧 代码如下: using UnityE ...
- (转)iOS Wow体验 - 第七章 - 操作图例与触屏人机工学
本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第七章译文精选,其余章节将陆续放出.上一篇:Wow ...
- PHP设计模式笔记四:适配器模式 -- Rango韩老师 http://www.imooc.com/learn/236
适配器模式 1.适配器模式,可以将截然不同的函数接口封装成统一的API 2.实际应用举例,PHP的数据库操作有mysql.mysqli.pdo三种,可以用适配器模式统一成一致,类似的场景还有cache ...
- JMeter创建FTP测试
FTP服务主要提供上传和下载功能.有时间需要我们测试服务器上传和下载的性能.在这里我通过JMeter做一个FTP测试计划的例子. * 使用的是JMeter2.4版本. * 测试的服务器是IP:124. ...
- Android应用盈利广告平台的嵌入方法详解
一.如何学习Android android开发(这里不提platform和底层驱动)你需要对Java有个良好的基础,一般我们用Eclipse作为开发工具.对于过多的具体知识详细介绍我这里不展开,我只 ...
- IEquatable(T) interface in .Net
原文:http://weblogs.asp.net/pawanmishra/iequatable-t-interface-in-net 泛型方法: public static bool AreEqua ...