From bc6189e9e9898c72180f1787f7614e0c95d0181f Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Sun, 10 Mar 2024 17:50:38 +0800 Subject: [PATCH] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0198.打家劫舍新增C语言实现 --- problems/0198.打家劫舍.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index a7bc4c99..480222ef 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -315,6 +315,31 @@ function rob(nums: number[]): number { }; ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int rob(int* nums, int numsSize) { + if(numsSize == 0){ + return 0; + } + if(numsSize == 1){ + return nums[0]; + } + // dp初始化 + int dp[numsSize]; + dp[0] = nums[0]; + dp[1] = max(nums[0], nums[1]); + for(int i = 2; i < numsSize; i++){ + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); + } + return dp[numsSize - 1]; +} +``` + + + ### Rust: ```rust @@ -339,3 +364,4 @@ impl Solution { +