标签:
/* * 140. Word Break II * 2016-5-23 by Mingyang * 这个题目用backtracking也可以,注意这里的string累积的形式 * newItem就是积累的String */ public ArrayList<String> wordBreak(String s, Set<String> dict) { ArrayList<String> res = new ArrayList<String>(); if (s == null || s.length() == 0) return res; helper(s, dict, 0, "", res); return res; } private void helper(String s, Set<String> dict, int start, String item, ArrayList<String> res) { if (start >= s.length()) { res.add(item); return; } StringBuilder str = new StringBuilder(); for (int i = start; i < s.length(); i++) { str.append(s.charAt(i)); if (dict.contains(str.toString())) { String newItem = item.length() > 0 ? (item + " " + str .toString()) : str.toString(); helper(s, dict, i + 1, newItem, res); } } }
标签:
原文地址:http://www.cnblogs.com/zmyvszk/p/5522477.html