From 216f9db871f5c08a8b44573c678eab3915aa9f49 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Sat, 9 Mar 2024 17:15:37 +0800 Subject: [PATCH] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D=E6=8B=86?= =?UTF-8?q?=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0139.单词拆分新增C语言实现 --- problems/0139.单词拆分.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index d93288ae..a3d59ec7 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -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 { -