house robber follow up
2016-08-04 15:08
323 查看
输出抢劫的house
public static void main(String[] args) { // rob(new int[]{4,1,2,7,5,3,1}); // rob(new int[]{1,1,3,6,7,10,7,1,8,5,9,1,4,4,3}); // rob(new // int[]{183,219,57,193,94,233,202,154,65,240,97,234,100,249,186,66,90,238,168,128,177,235,50,81,185,165,217,207,88,80,112,78,135,62,228,247,211}); rob(new int[] { 2, 7, 9, 3, 1 }); } public static List<Integer> rob(int[] nums) { if (nums == null || nums.length == 0) { return new LinkedList<>(); } List<Integer> path1 = new LinkedList<>(); List<Integer> path2 = new LinkedList<>(); boolean first = true; int[] maxValue = new int[nums.length + 1]; maxValue[1] = nums[0]; path1.add(1); for (int i = 2; i <= nums.length; i++) { if (maxValue[i - 2] + nums[i - 1] > maxValue[i - 1]) { maxValue[i] = maxValue[i - 2] + nums[i - 1]; } else { maxValue[i] = maxValue[i - 1]; } if (path1.get(path1.size() - 1) == i - 1) { path2.add(i); if (i == nums.length) { first = false; } } else { path1.add(i); } } if (first) { return path1; } else { return path2; } }
相关文章推荐
- 理解js的prototype原型对象
- 拥抱.NET Core,学习.NET Core的基础知识补遗
- lua(3)-string字符串&math数学库
- php 遍历
- java web开发在web.xml配置<mime-mapping>实现浏览器根据文件类型自动打开
- MyBatis常见错误(3)
- 腾讯2016研发工程师编程题微信红包
- PyQt5教程-18-窗口分隔
- Android 自定义View 带你飞(二)
- js号码邮箱手机号验证以及身份证
- MYBATIS常见错误(1)
- Asp.net MVC中使用KindEditor上传本地图片的注意事项
- 丑小鸭的故事--ruby中的方法
- bash基础特性及常用命令
- java中的错误集合
- string 类型详解
- VirtualBox: Failed to open a session for the virtual machine……
- 串口编程
- iOS编译问题的N种可能
- 蝉时雨.docx