【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 ...
随机推荐
- ubuntu14.04 + cocos2d-x-2.2.6 + eclipse发布android + Qt Creator4
先把需要的东西准备好,打开控制台,执行以下语句: sudo apt--jdk lib32z1 lib32ncurses5 lib32bz2- 接下来,准备好cocos2d-x-2.2.6和 andro ...
- HBase二级索引的设计(案例讲解)
摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowK ...
- Windows系统下nodejs安装及配置
关于nodejs中文站,眼下活跃度最好的知识站应该是http://www.cnodejs.org/ ,而http://cnodejs.org/则活跃度较低.Express.js是nodejs的一个MV ...
- 使用PLSql连接Oracle时报错ORA-12541: TNS: 无监听程序
非常多时候为了优化我们的启动项把oracle的服务禁止了.但是重新启动启动之后使用PLSQL登陆oracle时会出现无监听程序,这说明我们有一些服务没有启动.我们先查看一下oracle的服务是否启动, ...
- android面试题之七
三十六.请解释下在单线程模型中Message.Handler.Message Queue.Looper之间的关系. 简单的说,Handler获取当前线程中的looper对象,looper用来从存放Me ...
- 事关Animation Tree的工作随笔(二)
上回说到,游戏项目中客观会遇到逻辑状态的复杂性和动画状态的单一性之间的矛盾,那么Animation Tree是如何解决这个问题的呢? 这又需要引入一个定律:就是逻辑状态无论有多么复杂,但一套逻辑状态组 ...
- Oracle 监听配置详解(转载)
使用ORACLE的SQL*Net V.2连接客户机和服务器,需配置每个客户机和服务器.在服务器上需配置的文件有:listener.ora./etc/services,在客户机上需配置tnsna ...
- cocos2dx-触摸事件
//创建一个精灵 Sprite *spriteTest = Sprite::create("1.png"); spriteTest->setPosition(Point(,) ...
- 第一次写博客,关于前端开发deMVC在js中的应用
对前端MVC MVC分别是model.view.controller的缩写,模型.视图.控制器.这些更加偏向于后台,在以前MVC是只属于后台的.当然随着技术的进步,前端的大牛们将后台的一些东西应用于前 ...
- hdu 油菜花王国
Problem Description 程序设计竞赛即将到来,作为学校ACM集训队主力,小明训练一直很努力.今天天气不错,教练也心情大好,破例给各位队员放假一天,小明就骑着自己的小电驴到郊外踏青去了. ...