【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 ...
随机推荐
- 简单计算器(Android)
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKC
- JavaScript进阶篇 - -第1章 系好安全带
第1章 系好安全带 html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", ...
- iOS 语音识别使用讯飞报错
You must rebuild it with bitcode enabled(Xcode setting ENABLE_BITCODE), obtain an updated library fr ...
- html checkbox全选或者全不选
/* 全选或全不选 */ function CheckedAllOrNo() { var arr = $(':checkbox'); for (var i = 1; i < arr.length ...
- MyBatis初学者配置
小配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC &qu ...
- js将日期格式的时候转换成时间搓
自己写的一个方法 function split_time(time){//将当前时间转换成时间搓 例如2013-09-11 12:12:12 var arr=time.split(" ...
- Android高级图片滚动控件,编写3D版的图片轮播器
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17482089 大家好,好久不见了,最近由于工作特别繁忙,已经有一个多月的时间没写博 ...
- C/C++笔试准备(2)
问题:编辑距离,是指将一个字符串变为另一个字符串,仅可以3种操作:修改一个字符,删除一个字符,插入一个字符.the变成that:删除e,插入a,插入t.20’ 实现编辑距离算法. 解算:利用动态规划的 ...
- mysql判断某个字符串是否包含另一个
like SELECT * FROM wx_webauth_userinfo where city LIKE "%台%";"; 结果: 函数find_in_set(str ...
- Android字数限制的EditText实现方案研究
在应用开发中,有时需要实现有字数限制的EditText,首先来分析下市面上存在的类似实现方案吧,好有个感性的认识. [方案一:腾讯微博] 每个中文字符算一个字数,每两个英文字符算一个字数,当用户输入内 ...