首页 > 其他 > 详细

[LeetCode]Word Break II

时间:2015-12-06 13:07:15      阅读:109      评论:0      收藏:0      [点我收藏+]
public class Solution {
    
    public List<String> wordBreak(String s, Set<String> wordDict) {
        return helper(s, wordDict, new HashMap<String, List<String>>());
    }
    public List<String> helper(String s, Set<String> wordDict, HashMap<String, List<String>> map) {
        if (map.containsKey(s)) {
            return map.get(s);
        }
        List<String> result = new ArrayList<String>();
        if (s.length() == 0) {
            return result;
        }
        for (String str : wordDict) {
            if (str.length() > s.length()) {
                continue;
            }
            if (str.equals(s.substring(0, str.length()))) {
                if (str.equals(s)) {
                    result.add(str);
                }
                List<String> list = helper(s.substring(str.length()), wordDict, map);
                for (String ss : list) {
                    result.add(str + " " + ss);
                }
            }
        }
        map.put(s, result);
        return result;
    }
}

 

[LeetCode]Word Break II

原文:http://www.cnblogs.com/vision-love-programming/p/5023326.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!