parent
216f9db871
commit
bc6189e9e9
|
|
@ -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 {
|
|||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue