mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Update 0139.单词拆分.md
0139.单词拆分新增C语言实现
This commit is contained in:
@ -498,6 +498,33 @@ function wordBreak(s: string, wordDict: string[]): boolean {
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
bool wordBreak(char* s, char** wordDict, int wordDictSize) {
|
||||
int len = strlen(s);
|
||||
// 初始化
|
||||
bool dp[len + 1];
|
||||
memset(dp, false, sizeof (dp));
|
||||
dp[0] = true;
|
||||
for (int i = 1; i < len + 1; ++i) {
|
||||
for(int j = 0; j < wordDictSize; j++){
|
||||
int wordLen = strlen(wordDict[j]);
|
||||
// 分割点是由i和字典单词长度决定
|
||||
int k = i - wordLen;
|
||||
if(k < 0){
|
||||
continue;
|
||||
}
|
||||
// 这里注意要限制长度,故用strncmp
|
||||
dp[i] = (dp[k] && !strncmp(s + k, wordDict[j], wordLen)) || dp[i];
|
||||
}
|
||||
}
|
||||
return dp[len];
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
@ -521,4 +548,3 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user