Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c427ed

Browse files
Update 0139.单词拆分.md
添加 python3 版本代码
1 parent 2c24d81 commit 1c427ed

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

‎problems/0139.单词拆分.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ class Solution {
252252

253253
Python:
254254

255+
```python3
256+
class Solution:
257+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
258+
'''排列'''
259+
dp = [False]*(len(s) + 1)
260+
dp[0] = True
261+
# 遍历背包
262+
for j in range(1, len(s) + 1):
263+
# 遍历单词
264+
for word in wordDict:
265+
if j >= len(word):
266+
dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j])
267+
return dp[len(s)]
268+
```
269+
270+
271+
255272

256273
Go:
257274
```Go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /