191:位1的个数

This commit is contained in:
huangge1199@hotmail.com 2021-07-19 23:39:34 +08:00
parent cd73435799
commit 3445011c87
2 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,61 @@
//你是一个专业的小偷计划偷窃沿街的房屋每间房内都藏有一定的现金影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统如果两间相邻的房屋在同一晚上
//被小偷闯入系统会自动报警
//
// 给定一个代表每个房屋存放金额的非负整数数组计算你 不触动警报装置的情况下 一夜之内能够偷窃到的最高金额
//
//
//
// 示例 1
//
//
//输入[1,2,3,1]
//输出4
//解释偷窃 1 号房屋 (金额 = 1) 然后偷窃 3 号房屋 (金额 = 3)
//  偷窃到的最高金额 = 1 + 3 = 4
//
// 示例 2
//
//
//输入[2,7,9,3,1]
//输出12
//解释偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9)接着偷窃 5 号房屋 (金额 = 1)
//  偷窃到的最高金额 = 2 + 9 + 1 = 12
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 100
// 0 <= nums[i] <= 400
//
// Related Topics 数组 动态规划
// 👍 1559 👎 0
package leetcode.editor.cn;
//198:打家劫舍
class HouseRobber {
public static void main(String[] args) {
//测试代码
Solution solution = new HouseRobber().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int rob(int[] nums) {
int[] dp = new int[2];
dp[1] = nums[0];
for (int i = 1; i < nums.length; i++) {
int tmp = dp[1];
dp[1] = nums[i] + dp[0];
dp[0] = Math.max(tmp, dp[0]);
}
return Math.max(dp[1], dp[0]);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,32 @@
<p>你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,<strong>如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警</strong></p>
<p>给定一个代表每个房屋存放金额的非负整数数组,计算你<strong> 不触动警报装置的情况下 </strong>,一夜之内能够偷窃到的最高金额。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>[1,2,3,1]
<strong>输出:</strong>4
<strong>解释:</strong>偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
  偷窃到的最高金额 = 1 + 3 = 4 。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>[2,7,9,3,1]
<strong>输出:</strong>12
<strong>解释:</strong>偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
  偷窃到的最高金额 = 2 + 9 + 1 = 12 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 400</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div>\n<div><li>👍 1559</li><li>👎 0</li></div>