Skip to content

Commit 216f9db

Browse files
committed
Update 0139.单词拆分.md
0139.单词拆分新增C语言实现
1 parent 6bae304 commit 216f9db

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

problems/0139.单词拆分.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,33 @@ function wordBreak(s: string, wordDict: string[]): boolean {
498498
};
499499
```
500500

501+
### C
502+
503+
```c
504+
bool wordBreak(char* s, char** wordDict, int wordDictSize) {
505+
int len = strlen(s);
506+
// 初始化
507+
bool dp[len + 1];
508+
memset(dp, false, sizeof (dp));
509+
dp[0] = true;
510+
for (int i = 1; i < len + 1; ++i) {
511+
for(int j = 0; j < wordDictSize; j++){
512+
int wordLen = strlen(wordDict[j]);
513+
// 分割点是由i和字典单词长度决定
514+
int k = i - wordLen;
515+
if(k < 0){
516+
continue;
517+
}
518+
// 这里注意要限制长度,故用strncmp
519+
dp[i] = (dp[k] && !strncmp(s + k, wordDict[j], wordLen)) || dp[i];
520+
}
521+
}
522+
return dp[len];
523+
}
524+
```
525+
526+
527+
501528
### Rust:
502529
503530
```rust
@@ -521,4 +548,3 @@ impl Solution {
521548
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
522549
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
523550
</a>
524-

0 commit comments

Comments
 (0)