Merge pull request #2112 from fwqaaq/patch-32

Update 0139.单词拆分.md about rust
This commit is contained in:
程序员Carl 2023-06-15 09:32:22 +08:00 committed by GitHub
commit 2d95c770a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -496,7 +496,24 @@ function wordBreak(s: string, wordDict: string[]): boolean {
};
```
Rust:
```rust
impl Solution {
pub fn word_break(s: String, word_dict: Vec<String>) -> bool {
let mut dp = vec![false; s.len() + 1];
dp[0] = true;
for i in 1..=s.len() {
for j in 0..i {
if word_dict.iter().any(|word| *word == s[j..i]) && dp[j] {
dp[i] = true;
}
}
}
dp[s.len()]
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">