网站包503错误,wordpress dedecms整合,女做受网站,国内免费的ip地址给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict#xff0c;在字符串中增加空格来构建一个句子#xff0c;使得句子中所有的单词都在词典中。返回所有这些可能的句子。
说明#xff1a;
分隔时可以重复使用字典中的单词。 你可以假设字典中没有重复的单词。 …给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict在字符串中增加空格来构建一个句子使得句子中所有的单词都在词典中。返回所有这些可能的句子。
说明
分隔时可以重复使用字典中的单词。 你可以假设字典中没有重复的单词。 示例 1
输入: s “catsanddog” wordDict [“cat”, “cats”, “and”, “sand”, “dog”] 输出: [ “cats and dog”, “cat sand dog” ]
代码
class Solution {MapInteger,ListListString listMapnew HashMap();public ListString wordBreak(String s, ListString wordDict) {Trie trienew Trie();SetInteger setnew HashSet();for(String c:wordDict) {set.add(c.length());trie.insert(c);}ListString retnew ArrayList(); ListListString resssawordBreak(s,0,trie,set);for(java.util.ListString list:resss) ret.add(String.join( ,list));//添加空格return ret;}public ListListString awordBreak(String s, int loc,Trie trie,SetInteger set) {if(!listMap.containsKey(loc)){ListListString tempnew LinkedListListString();if(locs.length())temp.add(new ArrayList());for(int c:set)//测试字典的所有单词{if(loccs.length()){String needs.substring(loc,locc);if(trie.search(need)){//该单词合法ListListString nexts awordBreak(s,locc,trie,set);//递归获取字符串后部分返回的单词序列for(ListString list:nexts)//将不同的单词序列与当前单词连接{LinkedListString nextnew LinkedList(list);next.offerFirst(need);temp.add(next);}}}}listMap.put(loc,temp);}return listMap.get(loc);}class TrieNode {//字典树private TrieNode[] links;private final int r26;private boolean isEnd;public TrieNode() {this.links new TrieNode[r];}public boolean contains(char c) {return links[c-a]!null;}public void put(char c,TrieNode node) {links[c-a]node;}public TrieNode get(char c) {return links[c-a];}public boolean isEnd() {return isEnd;}public void setEnd() {isEnd true;}}class Trie{private TrieNode root;public Trie() {this.root new TrieNode();}public void insert(String s){TrieNode curroot;for(int i0;is.length();i){if(!cur.contains(s.charAt(i))){cur.put(s.charAt(i),new TrieNode());}curcur.get(s.charAt(i));}cur.setEnd();}public boolean search(String s){TrieNode curroot;for(int i0;is.length();i){if(cur.contains(s.charAt(i))){curcur.get(s.charAt(i));}else return false;}return cur.isEnd;}}
}