【leetcode】139. 单词拆分

 

bool wordBreak(char * s, char ** wordDict, int wordDictSize){
    int i=0, j, len=strlen(s);
    int stack[10000], left=0, right=0, val=0;
    stack[right++]=0;
    int hash[1000]={0};
    while(left<right){
        i=stack[left++];
        if (hash[i]==1) 
            continue;
        for (j=0; j<wordDictSize; j++)
        {
            if(s+i == strstr(s+i,wordDict[j])){
                val=i+strlen(wordDict[j]);
                if(val == len)
                    return true;
                if (hash[val]==0)                    
                    stack[right++]=i+strlen(wordDict[j]);
            }
        }
        if(j==wordDictSize)
            hash[i]=1;
    }
    return false;
}
bool wordBreak(char * s, char ** wordDict, int wordDictSize){
    int i=0, j, len=strlen(s);
    int stack[10000], left=0, right=0, val=0;
    stack[right++]=0;
    int hash[1000]={0};
    while(left<right){
        i=stack[left++];
        if (hash[i]==1) 
            continue;
        for (j=0; j<wordDictSize; j++)
        {
            if(s+i == strstr(s+i,wordDict[j])){
                val=i+strlen(wordDict[j]);
                if(val == len)
                    return true;
                if (hash[val]==0)                    
                    stack[right++]=i+strlen(wordDict[j]);
            }
        }
        if(j==wordDictSize)
            hash[i]=1;
    }
    return false;
}

 

posted @ 2020-12-22 11:52  温暖了寂寞  阅读(101)  评论(0编辑  收藏  举报