leetcode每日刷题计划-简单篇day12
Num 125 验证回文串 Valid Palindrome
非常有收货的一道题嘻嘻嘻,本来是考试期间划水挑的题,坑点有点多
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。第一个是注意对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>=48 && x<=57)||(x>=65 && x<=90) || (x>=97 && x<=122)) 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)==32 && x1>=65 && x2>=65) return true; return false; } bool isPalindrome(string s) { int temp1,temp2; temp1=0; temp2=s.length()-1; 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; } };View Code
更多精彩