leetcode——(4)打家劫舍

代碼

  • 思路是對的,但是代碼好醜…
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;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章