Word Break I,II, Triangle,Palindrome Partitioning 动态规划 DP

 动态规划 DP


Word Break

Given a string s and a dictionary of words dict, determine ifs can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

============================================================

本题如果暴力,O(2^n),肯定超时,详见https://oj.leetcode.com/discuss/7181/what-time-complexity-recursive-solution-this-problem-how-get

动态规划方法(仅提示)

开一个长度为字符串s长度的bool数组,初始化全部为false,记录从各个字母为起点到结尾的子串是否可以在字典中有分割方案,有就把数组的该位置记为true

从数组的最后一位开始遍历,计算true or false,计算到第一位则结束,最终返回第一位bool值即可

细节请自行思考


--------------------------------------------------------------------------------------------

Word Break II

思路与第一题一样,只是这次需要记录划分结果,并且全部遍历,中途不要continue和break即可

--------------------------------------------------------------------------------------------

Palindrome Partitioning

思路同上

--------------------------------------------------------------------------------------------

Triangle

比较简单的一道动态规划,自底向上计算即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值