leetcode 单词拆分 II java

题目:

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

  • 分隔时可以重复使用字典中的单词。
  • 你可以假设字典中没有重复的单词。

示例 1:

输入:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
[
  "cats and dog",
  "cat sand dog"
]

示例 2:

输入:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
输出:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
解释: 注意你可以重复使用字典中的单词。

示例 3:

输入:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
输出:
[]

解题:

class Solution {
    private Map<String, List<String>> map = new HashMap<>();
    public List<String> wordBreak(String s, List<String> wordDict) {
        if (map.containsKey(s)) //如果包含 则直接返回s
            return map.get(s);
        List<String> list = new ArrayList<>();
        if (s.length() == 0) {
            list.add("");
            return list;
        }
        for (String word : wordDict) {
            if (s.startsWith(word)) {//判断s是否含有word的前缀
                List<String> tmpList = wordBreak(s.substring(word.length()), wordDict);
                for (String tmp : tmpList)
                    list.add(word + (tmp.equals("") ? "" : " ") + tmp);//空的话则""结尾
            }
        }
        map.put(s, list);//记录可以拆分的字符串,并且记录拆分的方法
        return list;
    }
}

原文地址:https://www.cnblogs.com/yanhowever/p/10950763.html

时间: 2024-10-08 16:07:13

leetcode 单词拆分 II java的相关文章

单词拆分II

#单词拆分II#给一字串s和单词的字典dict,在字串中增加空格来构建一个句子,并且所有单词都来自字典.#返回所有有可能的句子.#EXAMPLE:#给一字串lintcode,字典为["de", "ding", "co", "code", "lint"]#结果为["lint code", "lint co de"]. class Solution:    "&

leetcode 90 Subsets II ----- java

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这道题就是Sub

leetcode 丑数 II java

题目: 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10输出: 12解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数.说明: 1 是丑数.n 不超过1690. 解题: 使用三指针 class Solution { public int nthUglyNumber(int n) { int min2 = 0; int min3 = 0; int min5 = 0; int []result =

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

leetcode 119 Pascal&#39;s Triangle II ----- java

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl

[LeetCode] 139. 单词拆分 ☆☆☆(动态规划 回溯)

139. 单词拆分 描述 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词.你可以假设字典中没有重复的单词.示例 1: 输入: s = "leetcode", wordDict = ["leet", "code"]输出: true解释: 返回 true 因为 "leetcode" 可以被拆分成 "

8.18 [LeetCode 52] N-Queens II

[LeetCode 52] N-Queens II | COMMENTS Question link Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Stats Frequency 3 Difficulty 4 Adjusted Difficulty 2 Time to use ——– Ratin

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict