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
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    conda安装使用
    数据库删除后台代码
    表格显示数据库(html和php混编)
    唯一用户名jquery和PHP代码
    //阿贾克斯提交数据库
    //向数据库添加数据(form表单提交)
    //conn数据库配置
    Css 变量
    input标签让光标不出现
    Es6Class
  • 原文地址:https://www.cnblogs.com/tingxilin/p/10747823.html
Copyright © 2011-2022 走看看