leetcode 139. Word Break 深度优先遍历DFS按照index递归搜索 + 很棒的动态规划DP做法
2017-09-17 11:26
1016 查看
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = “leetcode”,
dict = [“leet”, “code”].
Return true because “leetcode” can be segmented as “leet code”.
UPDATE (2017/1/4):
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
这道题使用DFS很直觉,但是你会发现会出现问题,一些benchmark过不了,主要是类似aaaa、aaa的wordList和aaaaaaa的配对就会出现问题,所以我这里稍微修改了递归做法。
使用DP的思路如下:
dp[i]表示1到i的字符是可以分割的,那么
dp[i]=true 当且仅当 dp[j]=true && wordList包含字符串i到j。
直接看代码更直接。
这道题leetcode 472. Concatenated Words 连接字符串的判定 + 动态规划DP实现 是本题的一个升级,值得一起学习
代码如下:
下面是C++的做法,当你遇到字符串匹配的时候就该自然而然的想到使用DP动态规划来做,所以本题就该使用DP动态规划来做
代码如下:
For example, given
s = “leetcode”,
dict = [“leet”, “code”].
Return true because “leetcode” can be segmented as “leet code”.
UPDATE (2017/1/4):
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
这道题使用DFS很直觉,但是你会发现会出现问题,一些benchmark过不了,主要是类似aaaa、aaa的wordList和aaaaaaa的配对就会出现问题,所以我这里稍微修改了递归做法。
使用DP的思路如下:
boolean []dp = new boolean[s.length()+1]; dp[0] = true;
dp[i]表示1到i的字符是可以分割的,那么
dp[i]=true 当且仅当 dp[j]=true && wordList包含字符串i到j。
直接看代码更直接。
这道题leetcode 472. Concatenated Words 连接字符串的判定 + 动态规划DP实现 是本题的一个升级,值得一起学习
代码如下:
import java.util.List;
/*
* 你也能看出来使用DP和递归去做,但是就是做不出来
* http://www.cnblogs.com/reboot329/p/6168346.html * */
public class Solution
{
/*
* 下面是DFS做法,但是有问题,对于类似{aaaa,aaa}和aaaaaaa的benchmark会出现问题
*
* */
public boolean wordBreakByDFS(String s, List<String> wordDict)
{
return byDFS(s,wordDict,0);
}
boolean byDFS(String s, List<String> wordDict, int index)
{
if(index>=s.length())
return true;
else
{
for(int i=index+1;i<=s.length();i++)
{
String key=s.substring(index, i);
if(wordDict.contains(key))
{
wordDict.remove(key);
boolean res=byDFS(s, wordDict, i);
if(res==false)
wordDict.add(key);
}
}
return false;
}
}
/*
* dp[i]表示1-i的字符串是可以分割的
* dp[0]代表永远可分
* */
public boolean wordBreak(String s, List<String> wordDict)
{
boolean []dp = new boolean[s.length()+1]; dp[0] = true;
for(int i=1; i<=s.length();i++)
{
for(int j=0;j<i;j++)
{
//这个条件的意识是0到j可以分割,同时j到i又在wordDic里面,
//所以0到i可以分割,一旦确定可以分割就可以break了
if(dp[j] && wordDict.contains(s.substring(j, i)))
{
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
}
下面是C++的做法,当你遇到字符串匹配的时候就该自然而然的想到使用DP动态规划来做,所以本题就该使用DP动态规划来做
代码如下:
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <climits> using namespace std; class Solution { public: bool wordBreak(string s, vector<string>& wordDict) { set<string> st(wordDict.begin(),wordDict.end()); vector<bool> dp(s.length()+1,false); dp[0] = true; for (int i = 1; i <= s.length(); i++) { for (int j = 0; j < i; j++) { if ( dp[j] && st.find(s.substr(j, (i - 1)-j+1)) != st.end()) { dp[i] = true; break; } } } return dp[s.length()]; } };
相关文章推荐
- leetcode 97. Interleaving String(字符串交错出现) DFS深度优先遍历 + 很明显很经典的DP动态规划做法
- leetcode 140. Word Break II 深度优先搜索DFS + 很棒的动态规划DP 做法 + 记录前驱节点
- leetcode 131. Palindrome Partitioning 按照index做DFS深度优先遍历
- leetcode 526. Beautiful Arrangement 递归实现全排列 + 经典深度优先遍历DFS做法
- leetcode 40. Combination Sum II DFS深度优先搜索 + 按照index递归
- leetcode 417. Pacific Atlantic Water Flow可以到达两边的点+经典DFS深度优先遍历做法
- leetcode 79. Word Search DFS 单词搜索 + 深度优先遍历
- leetcode 464. Can I Win 100相加游戏 + 一个十分棒的深度优先遍历DFS的做法
- 638. Shopping Offers 深度优先遍历、递归、存储技术(基于递归的动态规划)
- leetcode 654. Maximum Binary Tree 递归构造最大二叉树 + 深度优先遍历DFS
- leetcode 508. Most Frequent Subtree Sum 子树和 + 一个简单的DFS深度优先遍历的做法
- leetcode 98. Validate Binary Search Tree DFS深度优先搜索 + 两个递归函数 + 一个错误做法
- leetcode 37. Sudoku Solver 一个经典的DFS深度优先搜索的做法
- leetcode 491. Increasing Subsequences所有的递增序列 + 一个典型的深度优先遍历DFS的做法
- leetcode 329. Longest Increasing Path in a Matrix 矩阵中寻找最长递增序列 + 一个典型的深度优先遍历DFS的做法
- leetcode 623. Add One Row to Tree二叉树添加指定深度元素+ 深度优先遍历DFS
- leetcode 652. Find Duplicate Subtrees Map记录查询 + 深度优先遍历DFS
- DFS深度优先搜索遍历
- leetcode 749. Contain Virus 消灭病毒建立墙 + 深度优先遍历DFS
- leetcode 530. Minimum Absolute Difference in 二叉搜索树的中序遍历 + 深度优先遍历DFS