[Leetcode] 140. Work Break II
来源:互联网 发布:arm单片机型号 编辑:程序博客网 时间:2024/05/22 17:34
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
import java.util.ArrayList;public class Solution { public List<String> wordBreak(String s, Set<String> dict) { ArrayList<String> result = new ArrayList<String>(); if(s == null || s.length() == 0) return result; ArrayList<String> list = new ArrayList<String>(); helper(s, dict, result, 0, list); return result; } private void helper(String s, Set<String> dict, ArrayList<String> result, int start, ArrayList<String> list){ if(start == s.length()){ StringBuffer sb = new StringBuffer(); for(int i = 0; i < list.size() - 1; i++){ sb.append(list.get(i) + " "); } sb.append(list.get(list.size() - 1)); result.add(sb.toString()); return; } for(int i = start; i < s.length(); i++){ String cur = s.substring(start, i + 1); if(dict.contains(cur)){ list.add(cur); helper(s, dict, result, i + 1, list); list.remove(list.size() - 1); } } }}
0 0
- [Leetcode] 140. Work Break II
- [leetcode] work break II
- [Leetcode] 139. Work Break
- [leetcode] 140.Word Break II
- LeetCode 140. Word Break II
- LeetCode 140. Word Break II
- leetcode.140. Word Break II
- LeetCode 140. Word Break II
- [LeetCode] 140. Word Break II
- 【LeetCode】140. Word Break II
- leetcode 140. Word Break II
- Leetcode 140. Word Break II
- leetcode 140. Word Break II
- Work Break II 解题心得
- LeetCode 140. Word Break II (DP+DFS)
- [leetcode] 140. Word Break II 解题报告
- [Leetcode] 140. Word Break II 解题报告
- [leetcode]140. Word Break II(Java)
- LeetCode - Candy
- UI 界面的编写工作,到底应该用 xib/storyboard 完成,还是用手写代码来完成?
- java反射机制
- 单片机的烧写原理
- Pretty print binary tree
- [Leetcode] 140. Work Break II
- Python 传递可变长参数
- windows下安装apache及php需要注意的问题
- 嵌入式 linux 第四课 内存管理单元MMU
- Decode Ways
- 如何解析SDP中包含的H.264的SPS和PPS串
- css滑动门技术
- hadoop 不使用hostname,使用ip遇到的问题(Datanode denied communication with namenode because hostname cann)及解决方案
- Calpuff-Server-GeoFile.cs