Merge pull request #388 from haofengsiji/update2

Update 0139.单词拆分.md
This commit is contained in:
程序员Carl
2021-06-12 16:48:52 +08:00
committed by GitHub

View File

@ -252,6 +252,23 @@ class Solution {
Python Python
```python3
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
'''排列'''
dp = [False]*(len(s) + 1)
dp[0] = True
# 遍历背包
for j in range(1, len(s) + 1):
# 遍历单词
for word in wordDict:
if j >= len(word):
dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j])
return dp[len(s)]
```
Go Go
```Go ```Go