# 198. House Robber [easy] (Python)

872人阅读 评论(0)

## 题目链接

https://leetcode.com/problems/house-robber/

## 题目原文

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.

## 思路方法

### 思路一

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 0
elif len(nums) < 2:
return max(nums[0], nums[-1])
money = [0]*len(nums)
money[0], money[1] = nums[0], max(nums[0], nums[1])
for i in xrange(2, len(nums)):
money[i] = max(nums[i] + money[i-2], money[i-1])
return money[len(nums)-1]

### 思路二

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
now = last = 0
for i in nums:
last, now = now, max(i+last, now)
return now

PS: 新手刷LeetCode，新手写博客，写错了或者写的不清楚还请帮忙指出，谢谢！

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：92860次
• 积分：1778
• 等级：
• 排名：千里之外
• 原创：90篇
• 转载：0篇
• 译文：0篇
• 评论：15条
博客专栏
 LeetCode解题报告 文章：90篇 阅读：92021
文章分类
最新评论