LeetCode刷题笔录Word Break II

Python环境搭建和LabVIEW中的调用 本文主要介绍Python相关的环境搭建、Anaconda的使用以及在LabVIEW中调用Python的方法。 阅读详情

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"].

这题要求出所有的结果,显然是要递归了。先写个brute force的解法。

public class Solution {
    public List<String> wordBreak(String s, Set<String> dict) {
        List<String> res = new ArrayList<String>();
        if(s.length() == 0 || s == null)
            return res;
        recursive(s, 0, dict, "", res);
        return res;
    }
    
    public void recursive(String s, int index, Set<String> dict, String string, List<String> res){
        if(index >= s.length()){
            res.add(string);
            return;
        }
        StringBuilder builder = new StringBuilder();
        for(int i = index; i < s.length(); i++){
            builder.append(s.charAt(i));
            if(dict.contains(builder.toString())){
                String str = string.isEmpty() ? builder.toString() : (string + " " + builder.toString());
                recursive(s, i + 1, dict, str, res);
            }
        }
    }
}


大数据集是通不过的。网上看到了一个剪枝的办法,就是利用DP的思想,如果某个点i在之前的搜索中没有得到结果,那么以后就不进行搜索了。

public class Solution {
    public List<String> wordBreak(String s, Set<String> dict) {
        List<String> res = new ArrayList<String>();
        if(s.length() == 0 || s == null)
            return res;
        boolean[] possible = new boolean[s.length() + 1];
        Arrays.fill(possible, true);
        recursive(s, 0, dict, "", res, possible);
        return res;
    }
    
    public void recursive(String s, int index, Set<String> dict, String string, List<String> res, boolean[] possible){
        if(index >= s.length()){
            res.add(string);
            return;
        }
        StringBuilder builder = new StringBuilder();
        for(int i = index; i < s.length(); i++){
            builder.append(s.charAt(i));
            //only perform search if there are possible solutions in level i + 1
            if(dict.contains(builder.toString()) && possible[i + 1]){
                String str = string.isEmpty() ? builder.toString() : (string + " " + builder.toString());
                int beforeSearch = res.size();
                recursive(s, i + 1, dict, str, res, possible);
                //if the result size is not changed after the search, then there is no solution at i
                if(beforeSearch == res.size())
                    possible[i + 1] = false;
            }
        }
    }
}


深入详解DICOM医学影像定位线相关知识:理解定位线的概念、定位线的作用以及定位线显示和计算原理 阅读详情

相关推荐

华为P812E光猫补全shell配置PON口模式

终于看到华为企业级的带2.5电口P812E横空出世了,相信很多发烧友也已经将此收入昂中。相信体验的同时,遇到的第一件事情就是配置PON口模式的问题,本文将带发烧基友解锁模式。

GUAIYU的博客 5725

Largest BST in a Binary Tree

一到面试题,挺有意思的。 简单做法是对于每个node都进行一遍isBST的测试,找出最大的那个。 public static int largestBSTNaive(TreeNode root){ if(isBST(root)){ return size(root); } return Math.max(largestBSTNaive(root.left), largestB

likecool21的专栏 1969

BEVFusion(mit)最强环境安装,部署复现

BEVFusion作为一项经典的工作,值得深入探索。然而由于配置环境比较麻烦,耽误了一些时间。经过几次成功配置之后,有了一些总结,在这里记录下来,供大家参考避坑,也方便自己下次查阅。

qq_45783225的博客 1万+

LeetCode刷题笔录Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit st

likecool21的专栏 1134

智能车数字图像处理算法入门及C语言实现

智能车图像的最最基本入门算法介绍及C语言实现,二值化 - 特征识别 - 补线 - 中心巡线 - 偏差曲率计算。

不想随波逐流的大学生一枚,我们每个人都在努力奔跑 5110

【数据集】waymo motion dataset介绍与数据解析可视化

waymo motion数据集下载,数据描述,数据特征和地图特征,数据解析并完成数据可视化

shuaixio的博客 5179

LeetCode刷题笔录Subsets

自己总是写不出递归的方法,只能用f

likecool21的专栏 1121

Ubuntu 20.04 系统备份和还原

最近大家Ubuntu开发环境,经常出现搭着搭着系统就挂了,于是开始琢磨了Ubuntu系统的备份和还原,以备不时之需,Linux环境搭建实在是有点太细,好多情况下,就算曾经搭建过一遍,再次搭建有些包还是忘了安装,做好备份是十分必要的。 经过了一番研究,Ubuntu备份有很多方式:根目录tar、dd、Systemback(1.0版本已经不维护了,2.0版本收费)等等,最终决定使用 —— Timesift, 因为方便一些。 文章参考:小马虎的救星——Timeshift Timeshift Timeshift,是L

Z_Paper的博客 2万+

LeetCode刷题笔录Unique Binary Search Trees

这里有个讲解(印度人口音真难懂)

likecool21的专栏 1043

面试官连夜整理:103道AI Agent面试题,答对一半直接加薪50%

维度AutoGPTLangGraphAutoGen控制流LLM 驱动的隐式循环代码层定义的显式状态图对话驱动的自动流转状态存储Prompt 上下文中(易丢失)结构化 State 对象(可持久化)对话历史(自动累积)分支能力依赖 LLM 的文本输出(不稳定)条件边 + 节点路由(确定性)多 Agent 对话自然分支(涌现式)回溯/回滚无原生支持,只能重新开始原生 Checkpoint 支持时间旅行无全局回滚,单 Agent 可重启人机协同仅人工在关键点介入(需要自定义)原生支持。

weixin_35774598的博客 620

SPI 总线信号完整性与时序一致性测试

SPI总线测试的核心目标是验证其电气与时序行为是否满足芯片规格要求,确保通信可靠性

硬核科技 328

LeetCode刷题笔录Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3

likecool21的专栏 1016

文末含资料链接和视频讲解!YOLOv8+RepVGG+QueryDet小目标检测终极指南:从理论到实战,突破遥感图像检测难题

QueryDet(Query Detection)机制代表了目标检测领域的一个重要范式转变。传统的目标检测方法基于anchor或anchor-free的密集预测,需要为图像中的每个可能位置生成预测。而QueryDet采用了稀疏查询的方式,通过少量的可学习查询向量来直接预测目标。 这种设计灵感来源于Transformer架构中的注意力机制。在自然语言处理中,Transformer通过query-key-value机制实现了高效的序列建模。QueryDet将这一思想引入到目标检测中,将检测问题转化为查询匹配问

FJN110的博客 384

LeetCode刷题笔录Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

likecool21的专栏 878

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode刷题笔录Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

likecool21的专栏 865

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode刷题笔录Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \

likecool21的专栏 864

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai

likecool21的专栏 855

LeetCode刷题笔录Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.

likecool21的专栏 786

LeetCode刷题笔录 Same Tree

public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) return true; else if(p == null || q == null) return f

likecool21的专栏 775

LeetCode刷题笔录Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]

likecool21的专栏 760

LeetCode刷题笔录 Pow(x,n)

Implement pow(x, n). z

likecool21的专栏 752

LeetCode刷题笔录Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devis

likecool21的专栏 750

求集合的交集/差集

先利用外部排序使两个磁盘文件有序,然后顺序读取两个文件,使用双指针求交集。在进阶 1 中,我们是通过双指针去做的,最坏情况下我们需要完整遍历。时,O(n log m) 可能明显优于 O(n + m)。的,这就使得我们可以通过从 1~1000 遍历来确定交集。,它们已经按非降序排序,请你返回两个数组的。和上一题的区别在于,这里需要找到重复的元素。返回一个数组,数组中的每个元素在。的每个字符串中都出现的共用字符(**注意:**列表中的整数可以按。较小,可以完整加载到内存,而。正整数组成的一个非空数组,按。

jyyyyx的算法之旅 111

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 352

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 201

LeetCode 每日一题 2026/11/2-2026/11/8

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步

AlphaTao的博客 175
上一篇: LeetCode刷题笔录Longest Valid Parentheses
下一篇: LeetCode刷题笔录Maximal Rectangle
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值