File tree Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -498,6 +498,33 @@ function wordBreak(s: string, wordDict: string[]): boolean {
498
498
};
499
499
```
500
500
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
+
501
528
### Rust:
502
529
503
530
```rust
@@ -521,4 +548,3 @@ impl Solution {
521
548
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
522
549
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
523
550
</a >
524
-
You can’t perform that action at this time.
0 commit comments