【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 ...
随机推荐
- 一个跨域请求的XSS续
之前讨论过,在解决post跨域请求时,采用iframe+本域代理页的形式,兼容性(当然是包括IE6啦)是最好的.上次提到,代理页面的作用是:执行本域下的回调函数.就是这个原因,给XSS带来了便利.详细 ...
- hdu2817之整数快速幂
A sequence of numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- c语言函数定义、函数声明、函数调用以及extern跨文件的变量引用
1.如果没有定义,只有声明和调用:编译时会报连接错误.undefined reference to `func_in_a'2.如果没有声明,只有定义和调用:编译时一般会报警告,极少数情况下不会报警告. ...
- Java 内存区域和GC机制-java概念理解
推荐几篇关于java内存介绍的文章 Java 内存区域和GC机制 http://www.cnblogs.com/hnrainll/archive/2013/11/06/3410042.html ...
- myeclipse实现Servlet实例(2) 继承GenericServet类实现,需要重写service方法
1.在myeclipse新建web project,配置Tomcat(在myeclipse的Window--preferences) 2.然后在src新建servlet文件( 此处放在com.tsin ...
- Win32/MFC的基本概念
一.MFC的基本概念 单文档.多文档和对话框框架的区别 MFC中的类继承图的基本框架 CView类与CDocument的关系 Onpaint()和Ondraw()的关系 hdc-cdc区别联系 RUN ...
- vue + vue-resource 跨域访问
使用vue + vue-resource进行数据提交,后台使用RESTful API的方式存取数据,搞了一天,终于把后台搞好了.进行联合调试时,数据不能提交,报403错误: XMLHttpReques ...
- JSF标签大全详解
1. JSF入门 藉由以下的几个主题,可以大致了解JSF的轮廓与特性,我们来看看网页设计人员与应用程序设计人员各负责什么. 1.1简介JSF Web应用程序的开发与传统的单机程序开发在本质上存在着太多 ...
- 配置linux中文
1.~/.bash_profile文件添加一下内容并执行source ~/.bash_profile export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK 2./etc ...
- [转]MVP模式开发
转自:http://www.jianshu.com/p/f7ff18ac1c31 基于面向协议MVP模式下的软件设计-(iOS篇) 字数9196 阅读505 评论3 喜欢11 基于面向协议MVP模式下 ...