zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    题解45 跳跃游戏 II
    《重构》第八章阅读总结
    Scala学习笔记一
    Java初学随笔
    染色算法总结
    BingMap
    Google Earth API 替换方案
    本周安排
    OleDb 内存泄露问题
    css3 布局
  • 原文地址:https://www.cnblogs.com/ganxiang/p/14172189.html
Copyright © 2011-2022 走看看