【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221
package y2019.Algorithm.dynamicprogramming.easy; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.dynamicprogramming.easy
* @ClassName: Rob
* @Author: xiaof
* @Description: 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 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.
*
* Example 1:
*
* Input: [1,2,3,1]
* Output: 4
* Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
* Total amount you can rob = 1 + 3 = 4.
* Example 2:
*
* Input: [2,7,9,3,1]
* Output: 12
* Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
* Total amount you can rob = 2 + 9 + 1 = 12.
* @Date: 2019/8/16 8:45
* @Version: 1.0
*/
public class Rob { public int solution(int[] nums) {
if (nums == null || nums.length <= 0) {
return 0;
}
//这题主要就是要发现规律,那就是选择盗窃的时候,当前室是否要抢劫进入
//那么区别就是rob(i) = max{rob(i-2) + curhouse, rob(i-1)}
int[] dp = new int[nums.length + 1];
dp[1] = nums[0];
for (int i = 2; i < dp.length; ++i) {
dp[i] = Math.max(dp[i - 2] + nums[i - 1], dp[i - 1]);
} return dp[nums.length];
}
}
package y2019.Algorithm.dynamicprogramming.medium; import java.util.List; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.dynamicprogramming.medium
* @ClassName: WordBreak
* @Author: xiaof
* @Description: 139. Word Break
* Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
* determine if s can be segmented into a space-separated sequence of one or more dictionary words.
*
* Note:
*
* The same word in the dictionary may be reused multiple times in the segmentation.
* You may assume the dictionary does not contain duplicate words.
* Example 1:
*
* Input: s = "leetcode", wordDict = ["leet", "code"]
* Output: true
* Explanation: Return true because "leetcode" can be segmented as "leet code".
* Example 2:
*
* Input: s = "applepenapple", wordDict = ["apple", "pen"]
* Output: true
* Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
* Note that you are allowed to reuse a dictionary word.
* Example 3:
*
* Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
* Output: false
* @Date: 2019/8/16 8:46
* @Version: 1.0
*/
public class WordBreak { public boolean solution(String s, List<String> wordDict) {
//我们把s当做一个地址的字符数组,每次获取到一个新的字符的时候
//判断是否可以和字典匹配成功,如果成功那么当前位置的值就是true(任意一个字符串)
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true; //如果字符串长度为0,那么默认为true for (int i = 1; i < dp.length; ++i) {
//遍历所有的字符,进行比较
for (String curs : wordDict) {
//比较当前字符位置
if (i >= curs.length()) {
//前面的字符也要比较确认没问题才能继续比较
if (dp[i - curs.length()]) {
//获取相同长度的字符进行比较,然后把前面剩下的字符再进行比较
String compare = s.substring(i - curs.length(), i);
if (compare.equals(curs)) {
dp[i] = true;
break;
}
}
}
} } return dp[s.length()];
}
}
package y2019.Algorithm.dynamicprogramming.medium; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.dynamicprogramming.medium
* @ClassName: MaximalSquare
* @Author: xiaof
* @Description: 221. Maximal Square
* Given a 2D binary matrix filled with 0's and 1's, find the largest square(正方形) containing only 1's and return its area.
*
* Example:
*
* Input:
*
* 1 0 1 0 0
* 1 0 1 1 1
* 1 1 1 1 1
* 1 0 0 1 0
*
* Output: 4
* @Date: 2019/8/16 8:46
* @Version: 1.0
*/
public class MaximalSquare { public int solution(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
}
//这题可以转换为求边长,只有一个点的上面,左边,和左上角都是1的时候,才进行长度加1,如果有一个反向的值不为1,那么就无法进行加一
//这个二维数组用来求边长
int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
int maxlen = 0; for (int i = 1; i < dp.length; ++i) {
for (int j = 1; j < dp[i].length; ++j) {
//首先判断当前位置是否为空,如果是那么就判断边长能否添加
if (matrix[i - 1][j - 1] == '1') {
dp[i][j] = Math.min(dp[i-1][j], Math.min(dp[i][j-1], dp[i-1][j-1])) + 1;
maxlen = Math.max(maxlen, dp[i][j]);
}
}
} return maxlen * maxlen;
}
}
【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221的更多相关文章
- Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)
Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...
- 【LEETCODE】68、动态规划,medium级别,题目:95、120、91
package y2019.Algorithm.dynamicprogramming.medium; /** * @ProjectName: cutter-point * @Package: y201 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- leetcode笔记 动态规划在字符串匹配中的应用
目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)
HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包 ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
随机推荐
- 退役III次后做题记录(扯淡)
退役III次后做题记录(扯淡) CF607E Cross Sum 计算几何屎题 直接二分一下,算出每条线的位置然后算 注意相对位置这个不能先搞出坐标,直接算角度就行了,不然会卡精度/px flag:计 ...
- 第4组 Alpha冲刺(1/4)
队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.安排好各个组员的任务 2.收集各个组员的进度 3.写页面 4.写博客 展示Gi ...
- eclipse 如何将文件编辑器窗口的背景填充为背景图片?
1.情景展示 文件编辑窗口的背景默认为白色. 我们知道,对于整日面对电脑的程序员来说,白色容易造成眼疲劳,而且对于眼睛的伤害比较大. 所以,eclipse添加了黑色主题. 切换成黑色主题 改变 ...
- 分析WordPress数据表之文章表(功能篇)
数据表分析 wp_posts(文章表) 表字段如下:ID(文章ID)post_author(文章作者名,我想可以是为用户名,也可以是用户ID)post_date(文章发布日期)post_date_gm ...
- 深度学习中loss总结
一.分类损失 1.交叉熵损失函数 公式: 交叉熵的原理 交叉熵刻画的是实际输出(概率)与期望输出(概率)的距离,也就是交叉熵的值越小,两个概率分布就越接近.假设概率分布p为期望输出,概率分布q为实际输 ...
- js控制网页窗口一打开就自动全屏
1.如果不需要开新窗口 在body区加入: <body onLoad= "javascript:window.resizeTo(screen.availWidth,screen.a ...
- python lambda表达式简单用法【转】
python lambda表达式简单用法 1.lambda是什么? 看个例子: g = lambda x:x+1 看一下执行的结果: g(1) >>>2 g(2) >>& ...
- odoo开发笔记 -- 单台物理服务器上,利用docker部署多套odoo应用
部署结构: 待更新! ----服务器硬件配置: 操作系统:ubuntu16.04-64bit CPU/内存:4核8G 1. 基础环境安装 nginx离线安装: docker环境安装: 2. 官方容器镜 ...
- 支付宝小程序开发——获取位置API没有城市区号的最佳处理方案
前言: 需要对城市区号进行判断,但是支付宝小程序提供的my.getLocation() API返回的数据中只有6位的城市行政代码,诸如:深圳(440300),并没有区号(0755),那么怎么办呢? 需 ...
- (转)How To Create a Sudo User on Ubuntu
转自:https://linuxize.com/post/how-to-create-a-sudo-user-on-ubuntu/ The sudo command is designed to al ...