leetcode每日刷题计划-简单篇day12

Num 125 验证回文串 Valid Palindrome

非常有收货的一道题嘻嘻嘻,本来是考试期间划水挑的题,坑点有点多

第一个是注意对temp1和temp2中途更新的判断

第二个是字符串频繁的作为参数出现,一次又一次的出现会爆内存,使用const string &s这样不复制(奇怪len等于0并不会报错,好像是因为之前复制的本来是string满的,不小心复制多了一对“”,然后爆string了)

还有就是专门有函数将字符串里面的大小写进行转换的

class Solution {
public:
bool ischar(int a,const string & s)
{
int x=(int)s[a];
if((x>= && x<=)||(x>= && x<=) || (x>= && x<=))
return true;
return false;
}
bool same(int a1,int a2,const string & s)
{
int x1=(int)s[a1];
int x2=(int)s[a2];
if(x1==x2)
return true;
if(abs(x1-x2)== && x1>= && x2>=)
return true;
return false;
}
bool isPalindrome(string s) {
int temp1,temp2;
temp1=;
temp2=s.length()-;
while(temp1<temp2)
{
while(!ischar(temp1,s))
{
temp1++;
if(temp1>=temp2)
return true;
}
while(!ischar(temp2,s))
{
temp2--;
if(temp1>=temp2)
return true;
}
cout<<temp1<<" "<<temp2<<endl;
if(temp1>=temp2)
return true;
if(!same(temp1,temp2,s))
return false;
temp1++;
temp2--;
}
return true;
}
};
上一篇:遥远的国度 bzoj3083


下一篇:使用ARM模板在Azure中国大规模部署DCOS集群