今際の国の呵呵君
Saturday, October 28, 2017
[LeetCode]Word Break
很直观的递归题目,但是backtracking会超时,我们要用DP来优化。DP[i]表示string的前i个字符能否成功break,我们有递推公式:
DP[i] = true, if我们有 j, where s[j, i]在字典里,并且DP[i] == true
否则,DP[i] = false
时间复杂度O(n^2),空间复杂度O(n),代码如下:
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment