zoukankan      html  css  js  c++  java
  • 213. House Robber II Java Solutions

    After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

    Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

    Credits:
    Special thanks to @Freezen for adding this problem and creating all test cases.

    Subscribe to see which companies asked this question

     1 public class Solution {
     2     public int rob(int[] nums) {
     3         if(nums == null || nums.length <1) return 0;
     4         if(nums.length == 1) return nums[0];
     5         if(nums.length == 2) return Math.max(nums[0],nums[1]);//将其分为两种情况:1.包括nums[0] 2.包括nums[nums.length-1]
     6         return Math.max(getRob(nums,0,nums.length-2), getRob(nums,1,nums.length-1));
     7     }
     8     
     9     public int getRob(int[] nums,int s,int e){
    10         int len = e - s + 1;
    11         int[] res = new int[len];
    12         res[0] = nums[s];
    13         res[1] = Math.max(nums[s],nums[s+1]);
    14         for(int i = 2;i<len;i++){
    15             res[i] = Math.max(res[i-2]+nums[s+i], res[i-1]);
    16         }
    17         return res[len-1];
    18     }
    19 }
  • 相关阅读:
    蓝牙的HFP协议笔记
    23种设计模式
    读QT5.7源码(三)Q_OBJECT 和QMetaObject
    实现私有化(Pimpl) --- QT常见的设计模式
    蓝牙Profile的概念和常见种类(转)
    git分支合并
    git log的常见用法
    QThread详解
    git查看某个文件的修改历史
    因为代理原因导致的NotSerializableException
  • 原文地址:https://www.cnblogs.com/guoguolan/p/5452887.html
Copyright © 2011-2022 走看看