mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0198.打家劫舍.md
0198.打家劫舍新增C语言实现
This commit is contained in:
@ -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:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -339,3 +364,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user