带标点的回文串判断
2016-06-29 17:08
302 查看
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 {
public:
void toa(string &s)
{
for(int i=0;i<s.size();i++)
{
if(s[i]>='A' &&s[i]<='Z')
s[i]=s[i]+32;
}
}
bool isalpha(char ch)
{
if((ch>='a' && ch<='z')|| (ch>='0'&&ch<='9'))
return true;
else
return false;
}
bool isPalindrome(string s) {
if(s.size()<=1)
return true;
int begin=0;
int end=s.size()-1;
toa(s);
while(begin<=end)
{
if(!(isalpha(s[begin])))
begin++;
else if((!isalpha(s[end])))
end--;
else if(s[begin]==s[end])
{
begin++;
end--;
}
else
{
return false;
}
}
return true;
}
};
递归算法
class Solution {
public:
void toa(string &s)
{
for(int i=0;i<s.size();i++)
{
if(s[i]>='A' &&s[i]<='Z')
s[i]=s[i]+32;
}
}
bool isalpha(char ch)
{
if((ch>='a' && ch<='z')|| (ch>='0'&&ch<='9'))
return true;
else
return false;
}
bool isPalindrome(string s) {
if(s.size()<=1)
return true;
int begin=0;
int end=s.size()-1;
toa(s);
if(!(isalpha(s[begin])))
return isPalindrome(s.substr(begin+1,end));
else if((!isalpha(s[end])))
return isPalindrome(s.substr(begin,end-1));
else if(s[begin]==s[end])
{
return isPalindrome(s.substr(begin+1,end-1));
}
else
{
return false;
}
}
};
网上的测试用例“ab"不成功,但是在vs中可以通过
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 {
public:
void toa(string &s)
{
for(int i=0;i<s.size();i++)
{
if(s[i]>='A' &&s[i]<='Z')
s[i]=s[i]+32;
}
}
bool isalpha(char ch)
{
if((ch>='a' && ch<='z')|| (ch>='0'&&ch<='9'))
return true;
else
return false;
}
bool isPalindrome(string s) {
if(s.size()<=1)
return true;
int begin=0;
int end=s.size()-1;
toa(s);
while(begin<=end)
{
if(!(isalpha(s[begin])))
begin++;
else if((!isalpha(s[end])))
end--;
else if(s[begin]==s[end])
{
begin++;
end--;
}
else
{
return false;
}
}
return true;
}
};
递归算法
class Solution {
public:
void toa(string &s)
{
for(int i=0;i<s.size();i++)
{
if(s[i]>='A' &&s[i]<='Z')
s[i]=s[i]+32;
}
}
bool isalpha(char ch)
{
if((ch>='a' && ch<='z')|| (ch>='0'&&ch<='9'))
return true;
else
return false;
}
bool isPalindrome(string s) {
if(s.size()<=1)
return true;
int begin=0;
int end=s.size()-1;
toa(s);
if(!(isalpha(s[begin])))
return isPalindrome(s.substr(begin+1,end));
else if((!isalpha(s[end])))
return isPalindrome(s.substr(begin,end-1));
else if(s[begin]==s[end])
{
return isPalindrome(s.substr(begin+1,end-1));
}
else
{
return false;
}
}
};
网上的测试用例“ab"不成功,但是在vs中可以通过
相关文章推荐
- firemonkey打开子窗体(匿名回调函数)
- mac端抓包工具——Charles使用
- 各种解压缩 linux .tar .gz .bz2 .bz .zip
- Java语言中this关键字的用法详解
- 基于Spark平台的电影推荐系统实现
- 19.Shell文件包含
- 利用霍夫梯度法进行圆检测的原理概要及OpenCV代码实现
- C++中的using
- java中相对路径,绝对路径问题总结(转)
- 利用viewPager实现导航界面
- CentOS安装MongoDB
- JavaSE 基础 第34节 继承关系
- 5分钟了解Mockito
- 将pdf转换成excel格式的方法
- 布隆过滤器(Bloom Filter)详解
- svn精细配置各个目录的权限
- 梯度与Roberts、Prewitt、Sobel、Lapacian算子
- 如何在HTML页面中,用JavaScript获取application的值?
- 最长上升子序列
- spring定时器时间配置规则