Word Break I II@LeetCode

Word Break

递归,基本属于暴力求解。但是纯暴力应该是过不了的,参考网上的办法,增加了一个unmatch集合,类型是HashSet,用来存放那些已经被验证过无法匹配的字符串,这样可以剪掉很多分支。

实现代码:

javapublic class Solution {
    private Set<String> unmatch = new HashSet<String>();

    public boolean wordBreak(String s, Set<String> dict) {
        for (String prefix : dict) {
            if (s.equals(prefix))
                return true;
            if (s.startsWith(prefix)) {
                String suffix = s.substring(prefix.length(), s.length());
                if (!unmatch.contains(suffix)) {
                    if (wordBreak(suffix, dict))
                        return true;
                    else
                        unmatch.add(suffix);
                }
            }
        }
        return false;
    }
}

Word Break II

解法基本差不多,无非是增加记录路径的要求。这里唯一需要注意的是,在前面我们只要一找到符合条件的组合即可返回了,在这题中,一定要遍历所有情况之后再返回。当然,还是用了一个mismatch来剪分支。

实现代码:

javapublic class Solution {
    public List<String> wordBreak(String s, Set<String> dict) {
        List<String> result = new LinkedList<String>();
        if (s.length() == 0)
            return result;
        generate(s, dict, new HashSet<String>(), result, new StringBuilder());
        return result;
    }

    private boolean generate(String s, Set<String> dict, Set<String> mismatch,
                             List<String> result, StringBuilder sentence) {
        if (s.length() == 0) {
            result.add(sentence.toString());
            return true;
        }
        boolean canBreak = false;
        for (String word : dict) {
            if (s.startsWith(word)) {
                String suffix = s.substring(word.length(), s.length());
                if (!mismatch.contains(suffix)) {
                    StringBuilder newSentence = new StringBuilder(sentence);
                    if (newSentence.length() != 0) newSentence.append(" ");
                    newSentence.append(word);
                    if (generate(suffix, dict, mismatch,
                            result, newSentence)) {
                        canBreak = true;
                    } else {
                        mismatch.add(suffix);
                    }
                }
            }
        }
        return canBreak;
    }
}

1.1k 声望
64 粉丝
0 条评论
推荐阅读
关于 C++ vector 的两个小 tips
本来这篇文章标题我想起成《关于 vector 的两个小坑》,后来想想,其实也不算是坑,还是自己对原理性的东西理解的没做那么透彻。工作中遇到的很多问题,后来归根到底都是基础不牢靠。

findingea阅读 1.5k

【每日一题】T 秒后青蛙的位置
1377. T 秒后青蛙的位置关键词:深度优先题目来源:1377. T 秒后青蛙的位置 - 力扣(Leetcode)题目描述 {代码...} 给你一棵由 n 个顶点组成的无向树,顶点编号从 1 到 n。青蛙从 顶点 1 开始起跳。规则如下:在...

字节幺零二四阅读 677

封面图
【算法竞赛】力扣周赛(节选)2022-04-30
力扣周赛(节选)2022-04-306404. 将数组清空关键词:树状数组、找规律题目来源:6404. 将数组清空 - 力扣(Leetcode)——力扣第 103 场双周赛第4题题目描述 {代码...} 给你一个包含若干 互不相同 整数的数组 nums...

字节幺零二四阅读 656

封面图
【每日一题】翻转子数组得到最大的数组值
给你一个整数数组 nums 。「数组值」定义为所有满足 0 &lt;= i &lt; nums.length-1 的 |nums[i]-nums[i+1]| 的和。

字节幺零二四阅读 608

封面图
【每日一题】摘水果
在一个无限的 x 坐标轴上,有许多水果分布在其中某些位置。给你一个二维整数数组 fruits ,其中 fruits[i] = [positioni, amounti] 表示共有 amounti 个水果放置在 positioni 上。fruits 已经按 positioni 升序排...

字节幺零二四阅读 543

封面图
leetcode 2413 最小偶倍数
今天这题比较简单,一行代码就解决了,思路其实简单,就是判断下当前数的奇偶性,如果是偶数,那么最小公倍数肯定就是数本身;如果是奇数,那么就是两个数的乘积。

MMzhe阅读 471

封面图
【每日一题】使二叉树所有路径值相等的最小代价
给你一个整数 n 表示一棵 满二叉树 里面节点的数目,节点编号从 1 到 n 。根节点编号为 1 ,树中每个非叶子节点 i 都有两个孩子,分别是左孩子 2 * i 和右孩子 2 * i + 1 。

字节幺零二四阅读 470

封面图
1.1k 声望
64 粉丝
宣传栏