zoukankan      html  css  js  c++  java
  • leetcode 字符串

    Implement strStr()

    Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

    class Solution {
    public:
        char *strStr(char *haystack, char *needle) {
            char *s,*t;
            int i=0,j=strlen(haystack)-strlen(needle);
            while(i++<=j)
            {
                s=haystack;
                t=needle;
                while((*s==*t)&&*s&&*t)
                {
                    t++;s++;
                }
                if(*t=='\0')return haystack;
                haystack++;
            }
            return NULL;
        }
    };
    

     数字与字符串

    Integer to Roman

    Given an integer, convert it to a roman numeral.

    Input is guaranteed to be within the range from 1 to 3999.

    class Solution {
    public:
        int a[7]={1,5,10,50,100,500,1000};
        char *s="IVXLCDM";
        string intToRoman(int num) 
        {
           string x="";
           for(int i=6;i>=0;i--)
           {
                if(i%2==0)
                {
                     int n=num/a[i];
                     if(n==4)
                     {
                         x=x+s[i]+s[i+1];
                         num-=a[i]*4;
                     }
                     else while(n-->0)
                     {
                         x+=s[i];
                         num-=a[i];
                     }
                }
                else
                {
                     int m=num-a[i];
                     if(m>=0)
                     {
                        int remain=m/a[i-1];
                        if(remain<4)
                        {
                            x=x+s[i];
                            num=num-a[i];
                        }
                        else 
                        {
                            x=x+s[i-1]+s[i+1];
                            num=num-9*a[i-1];
                        }
                     }
                }      
           }
           return x;
        }
    };
    

     Interleaving String

    用递归的方法,导致大集合超时

    class Solution {
    public:
        bool isInterleave(string s1, string s2, string s3) 
        {
            if(s3.size()!=s1.size()+s2.size())return false;
            if(s1.size()==0)
            {
               if(s3==s2)
                 return true;
                 return false;
            }
            if(s2.size()==0)
            {
               if(s3==s1)
                 return true;
                 return false;
            }
            if(s3[0]==s1[0]&&s3[0]!=s2[0])return isInterleave(s1.substr(1),s2,s3.substr(1));
            if(s3[0]==s2[0]&&s3[0]!=s1[0])return isInterleave(s1,s2.substr(1),s3.substr(1));
            if(s3[0]==s1[0]&&s3[0]==s2[0])return isInterleave(s1.substr(1),s2,s3.substr(1))
           ||isInterleave(s1,s2.substr(1),s3.substr(1));
           return false;      
        }
    };
    

     用二维动态规划

    二维数组v[i][j]的值表示能否用s1前i个字符和s2前j个字符组成s3前i+j个字符。

    class Solution {
    public:
        bool isInterleave(string s1, string s2, string s3) 
        {
            if(s3.size()!=s1.size()+s2.size())return false;
            vector<vector<bool>> v(s1.size()+1,vector<bool>(s2.size()+1,false));
            v[0][0]=true;
            for(int i=1;i<s1.size()+1;i++)  
            {  
                if(s1[i-1]==s3[i-1])  
                {  v[i][0]=true;}  
                else  
                    break;  
            }   
            for(int i=1;i<s2.size()+1;i++)  
            {  
                if(s2[i-1]==s3[i-1])  
                {  v[0][i]=true;}  
                else  
                    break;  
            }       
            for(int i=1;i<s1.size()+1;i++)  
            {  
                for(int j=1;j<s2.size()+1;j++)  
                {  
                    if(v[i-1][j]&& s3[i+j-1]==s1[i-1])  
                    {  
                        v[i][j]=true;  
                    }  
                    else if(v[i][j-1]&& s3[i+j-1]==s2[j-1])  
                    {  
                        v[i][j]=true;  
                    }  
                    else  
                    {  
                        v[i][j]=false;  
                    }  
                }  
            }  
            return v[s1.size()][s2.size()];  
        }  
    };  
    
  • 相关阅读:
    vue 路由嵌套 (子路由跳转报错或者失效解决方法)
    vue-cli3主题色系统
    vue项目哀悼日
    el-upload 文件上传显示进度
    课表
    uni-app 开发钉钉小程序
    Linux/Centos/Ubuntu crontab备份数据库
    【转】鲁迅为厦大题写校名,为啥4个字错了3个?专家:鲁迅故意写错的。
    计数单位资料汇总(个十百千兆京...,分厘毫丝忽微纤沙尘埃...)
    【转】如意算盘:中国的第五大发明
  • 原文地址:https://www.cnblogs.com/tgkx1054/p/3072938.html
Copyright © 2011-2022 走看看