代碼
- 思路是對的,但是代碼好醜…
int rob(int* nums, int numsSize){
int now_max;
int prior;
int medium;
int i;
if(numsSize == 0){
return 0;
}
for( i = 0; i < numsSize; i++){
if( i == 0 ){
now_max = nums[i];
prior = nums[i];
}
else if( i == 1 ){
now_max = nums[i] > prior?nums[i]:prior;
}
else if( i == 2){
medium = now_max;
now_max = now_max > (nums[i] + prior)? now_max: (nums[i] + prior);
}
else{
prior = medium;
medium = now_max;
now_max = medium > (nums[i] + prior)? medium: (nums[i] + prior);
}
}
return now_max;
}