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
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    docker 容器与主机之间的数据copy
    vim 中如何快速注释和取消注释
    java查找字符中的某个内容并替换
    linux正则表达式
    数据流重定向与管道命令
    linux杂七杂八
    linux变量
    redis常用命令操作
    redis基本操作介绍
    redis数据结构
  • 原文地址:https://www.cnblogs.com/tingxilin/p/10747823.html
Copyright © 2011-2022 走看看