博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] House Robber
阅读量:4563 次
发布时间:2019-06-08

本文共 1010 字,大约阅读时间需要 3 分钟。

 

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

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.

     

     这道题的重点就是在于要在不能将相邻两个house里的钱相加的情况下计算出所有的house里可相加的钱的最大值。

     个人觉得比较简单的方法就是设定两个变量,一个previousrobbed,一个perviousdidntrob,因为我们在考虑是否add这个house的money的时候必须要考虑它相邻的之前的那个house有木有被breakin。

      同样还有一些情况,比如这个house被rob了,它后面连着两个house也是可以都不被rob的,因此我们又需要需要用到Math.max()method了。

      代码如下。~

public class Solution {    public int rob(int[] nums) {        if(nums==null||nums.length==0){            return 0;        }        int didrob=0;        int dontrob=0;        for(int i=0;i

 

转载于:https://www.cnblogs.com/orangeme404/p/4754073.html

你可能感兴趣的文章
四平方和
查看>>
第十八周 12.27-1.2
查看>>
C# IP地址字符串和数值转换
查看>>
TCHAR和CHAR类型的互转
查看>>
常用界面布局
查看>>
C语言—— for 循环
查看>>
IBM lotus9.0测试版即将公测
查看>>
xml常用方法
查看>>
Cube Stacking(并差集深度+结点个数)
查看>>
AndroidStudio3更改包名失败
查看>>
jq 删除数组中的元素
查看>>
添加按键事件处理及事件处理的参数传递
查看>>
js URL中文传参乱码
查看>>
Leetcode 367. Valid Perfect Square
查看>>
UVALive 3635 Pie(二分法)
查看>>
win系统查看自己电脑IP
查看>>
Backup&recovery备份和还原 mysql
查看>>
全局变量、局部变量、静态全局变量、静态局部变量的区别
查看>>
一道面试题及扩展
查看>>
Unity 3D 我来了
查看>>