zoukankan      html  css  js  c++  java
  • 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>=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
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    作业要求 20201022-1 每周例行报告
    作业要求 20201015-3 每周例行报告
    20201008-1 每周例行报告
    20200924-1 每周例行报告
    总结
    20201126-1 每周例行报告
    20201120-1 每周例行报告
    20201112-1 每周例行报告
    20201105-1 每周例行报告
    20201029-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/tingxilin/p/10747823.html
Copyright © 2011-2022 走看看