198. 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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums
representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
中文释义
你是一个专业的窃贼,计划沿街偷窃房屋。每个房屋都藏有一定数量的金钱,唯一阻止你偷窃每个房屋的限制是,相邻的房屋有连接的安全系统,如果同一夜两个相邻的房屋被闯入,它将自动联系警察。
给定一个整数数组 nums
,代表每个房屋的金钱数量,返回今晚你可以在不触动警报的情况下偷窃的最大金钱量。
示例
- 示例 1:
- 输入:
nums = [1,2,3,1]
- 输出:
4
- 解释:偷窃第 1 个房屋 (金钱 = 1) 然后偷窃第 3 个房屋 (金钱 = 3)。
你可以偷窃的总金额 = 1 + 3 = 4。
- 输入:
- 示例 2:
- 输入:
nums = [2,7,9,3,1]
- 输出:
12
- 解释:偷窃第 1 个房屋 (金钱 = 2), 第 3 个房屋 (金钱 = 9) 和第 5 个房屋 (金钱 = 1)。
你可以偷窃的总金额 = 2 + 9 + 1 = 12。
- 输入:
限制条件
1 <= n