120. Triangle

我的解法用了一个二维数组,这样比较浪费空间。O(n*n)

但是标准答案自底向上,一是不需要进行特别判断,二是可以覆盖数组,则只需要O(n)的空间大小。

class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int size = triangle.size();
if(size == 1) return triangle.get(0).get(0);
int[][] dp = new int[size][size];
dp[0][0] = triangle.get(0).get(0);
int min = Integer.MAX_VALUE;
for(int i = 1; i < size; i++){
for(int j = 0; j <= i; j++){
dp[i][j] = triangle.get(i).get(j);
if(j == 0){
dp[i][j] += dp[i - 1][j];
}else if(j == i){
dp[i][j] += dp[i - 1][j - 1];
}else dp[i][j] += Math.min(dp[i - 1][j - 1], dp[i - 1][j]);
if(i == size - 1) min = Math.min(min, dp[i][j]);
}
}
return min;
}
}

他山之石:

class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int size = triangle.size();
int[] dp = new int[size + 1];
for(int i = size - 1; i >= 0; i--){
for(int j = 0; j < triangle.get(i).size(); j++){
dp[j] = Math.min(dp[j], dp[j+1]) + triangle.get(i).get(j);
}
}
return dp[0];
}
}

64. Minimun Path Sum

和上一题思路基本一致,采用了上一题的思路,只用了一个1d array。

class Solution {
public int minPathSum(int[][] grid) {
if(grid.length == 0) return 0;
int m = grid.length;
int n = grid[0].length;
int[] dp = new int[n];
for(int i = 0; i < m; i ++){
for(int j = 0; j < n; j++){
if(i == 0 && j == 0){
dp[j] = grid[0][0];
}else if(i == 0){
dp[j] = grid[i][j] + dp[j - 1];
}else if(j == 0){
dp[j] = grid[i][j] + dp[j];
}else dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];
}
}
return dp[n-1];
}
}

62 Unique Path

还是一样的思路, 可以稍微简化一下,有一些值例如 j = 0的情况可以不需要便利。

class Solution {
public int uniquePaths(int m, int n) {
if( m <= 0 || n <= 0) return 0;
int[] ans = new int[n];
ans[0] = 1;
for(int i = 0; i < m; i++){
for(int j = 1; j < n; j++){
if(i == 0){
ans[j] = ans[j - 1];
}else ans[j] = ans[j] + ans[j - 1];
}
}
return ans[n - 1];
}
}

91 Decode Ways

class Solution {
public int numDecodings(String s) {
int len = s.length();
if(len == 0) return 0;
int[] dp = new int[len + 1];
dp[0] = 1;
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for(int i = 2; i <= len; i++){
int first = Integer.parseInt(s.substring(i - 1, i));
int second = Integer.parseInt(s.substring(i - 2, i));
if(first > 0){
dp[i] += dp[i - 1];
}
if(second >= 10 && second <= 26){
dp[i] += dp[i - 2];
}
}
return dp[len];
}
}

这道题不难,但是有很多cornercase 需要处:

两位数不能大于26 不能小于10 否则无法构成两位数

一位数必须大于0

所以最好的方法还是分别取出两位数和个位数看是否符合条件,如果符合条件就加上。

LeetCode6 dp的更多相关文章

  1. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  2. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  3. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  4. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  9. android px转换为dip/dp

    /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...

随机推荐

  1. style中各种选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. IIS故障 应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误。该进程 ID 为“XXXX”。数据字段包含错误号。

    (尝试失败,但觉得有可行性) 参考https://www.cnblogs.com/qidian10/p/6028784.html https://yq.aliyun.com/articles/6434 ...

  3. [转载]Redux原理(一):Store实现分析

    写在前面 写React也有段时间了,一直也是用Redux管理数据流,最近正好有时间分析下源码,一方面希望对Redux有一些理论上的认识:另一方面也学习下框架编程的思维方式. Redux如何管理stat ...

  4. 2018-7-24-WPF-渲染级别

    title author date CreateTime categories WPF 渲染级别 lindexi 2018-07-24 18:46:27 +0800 2018-04-20 16:26: ...

  5. linux性能分析工具Ntop

  6. Java代码乱象!

    文章转载自公众号  阿里巴巴中间件 , 作者 陈昌毅 导读 查尔斯·狄更斯在<双城记>中写道:“这是一个最好的时代,也是一个最坏的时代.” 移动互联网的快速发展,出现了许多新机遇,很多创业 ...

  7. 265-Keystone II JESD204B 66AK2L06 评估模块 (现行) XEVMK2LX

    Keystone II JESD204B 66AK2L06 评估模块 (现行) XEVMK2LX 一. 板卡概述The XEVMK 2LX is a full-featured evaluation ...

  8. 开源安全:PE分析

    https://github.com/JusticeRage/Manalyze.git https://github.com/JusticeRage/Manalyze https://www.free ...

  9. Flutter的DateTime轉換的各種方法

    概述: 表示一个时间点 通过构造函数或解析格式化的字符串创建DateTime对象,并且符合ISO 8601标准的子集,小时是24小时制,范围在0-23之间 DateTime对象创建之后,将是固定不变的 ...

  10. bzoj2560 串珠子 状压DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2560 题解 大概是这类关于无向图的联通性计数的套路了. 一开始我想的是这样的,考虑容斥,那么就 ...