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()];  
    }  
};  

 

posted @ 2013-05-11 17:35  代码改变未来  阅读(145)  评论(0编辑  收藏  举报