package y2019.Algorithm.dynamicprogramming.medium;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.dynamicprogramming.medium
* @ClassName: NumDecodings
* @Author: xiaof
* @Description: 91. Decode Ways
* A message containing letters from A-Z is being encoded to numbers using the following mapping:
*
* 'A' -> 1
* 'B' -> 2
* ...
* 'Z' -> 26
* Given a non-empty string containing only digits, determine the total number of ways to decode it.
*
* Example 1:
*
* Input: "12"
* Output: 2
* Explanation: It could be decoded as "AB" (1 2) or "L" (12).
* Example 2:
*
* Input: "226"
* Output: 3
* Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
* @Date: 2019/8/15 8:54
* @Version: 1.0
*/
public class NumDecodings { public int solution(String s) {
if (s == null || s.equals("") || (s.length() == 1 && s.equals("0"))) {
return 0;
}
//我们发现这个串总最多一次可以使用2个字符,并且这两个字符组成的数要小于26或等于26才行
//那么我们可以发先要获取当前字符能组成的解码数可分为
//dp[n] = dp[n-1] + {if(2num <= 26}{dp[n-2} 只有满足使用最后2位数作为解码数字的时候才能加上不用这个2个字符可以组成的个数
int[] dp = new int[s.length() + 1];
dp[0] = 1;dp[1] = 1;
char[] sc = s.toCharArray(); for (int i = 2; i < dp.length; ++i) {
//i用来标识取s的前i个字符,还要判断这个字符不能是0,不然不能单个字符使用
if (sc[i - 1] != '0') { dp[i] = dp[i - 1];
}
//判断如果去除2个数的位置
int l = i - 2;
int value = Integer.valueOf(s.substring(l, i));
if (value <= 26 && value > 0) {
dp[i] += dp[i - 2];
}
}
return dp[s.length()];
} public static void main(String[] args) {
String s = "12";
String s2 = "226";
String s3 = "10"; NumDecodings fuc = new NumDecodings(); fuc.solution(s3); }
}
package y2019.Algorithm.dynamicprogramming.medium;

import java.util.List;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.dynamicprogramming.medium
* @ClassName: MinimumTotal
* @Author: xiaof
* @Description: 120. Triangle
* Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
*
* For example, given the following triangle
*
* [
* [2],
* [3,4],
* [6,5,7],
* [4,1,8,3]
* ]
* The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
* @Date: 2019/8/15 8:54
* @Version: 1.0
*/
public class MinimumTotal { public int solution(List<List<Integer>> triangle) {
//这题我们反向遍历,从底往上进行遍历最小值
//可以得知第k行第i个数的最小路径是minpath[k][i]=min{minpath[k+1][i], minpath[k+1][i+1]} + triangle[k][i]
int[][] minpath = new int[triangle.size() + 1][triangle.size() + 1];
for (int row = triangle.size() - 1; row >= 0; --row) {
//列遍历数据
for (int column = 0; column < triangle.get(row).size(); ++column) {
minpath[row][column] = Math.min(minpath[row + 1][column], minpath[row + 1][column + 1]) + triangle.get(row).get(column);
}
} return minpath[0][0];
}
}
package y2019.Algorithm.dynamicprogramming.medium;

import y2019.Algorithm.common.TreeNode;

import java.util.ArrayList;
import java.util.List; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.dynamicprogramming.medium
* @ClassName: GenerateTrees
* @Author: xiaof
* @Description: 95. Unique Binary Search Trees II
*
* Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
*
* Example:
*
* Input: 3
* Output:
* [
* [1,null,3,2],
* [3,2,null,1],
* [3,1,null,null,2],
* [2,1,3],
* [1,null,2,null,3]
* ]
* Explanation:
* The above output corresponds to the 5 unique BST's shown below:
*
* 1 3 3 2 1
* \ / / / \ \
* 3 2 1 1 3 2
* / / \ \
* 2 1 2 3
*
* @Date: 2019/8/15 8:54
* @Version: 1.0
*/
public class GenerateTrees { public List<TreeNode> solution(int n) { if (n == 0) return new ArrayList<>(); return backtruack(1, n);
} //因为要求出所有可能性,那么需要递归出所有结果
public List<TreeNode> backtruack(int l, int r) {
//我们需要进行操作的范围是l->r
List<TreeNode> res = new ArrayList<>();
//如果l>r超过了,那么直接返回一个空的集合,因为这个区间不可能组成一颗树
if (l > r) {
res.add(null);
return res;
}
//如果l == r 那么就返回以当前节点作为根的树
if (l == r) {
res.add(new TreeNode(l));
return res;
}
//其余情况把l->r的所有节点进行遍历,依次作为根节点进行组合
List<TreeNode> leftlist, rightlist;
for (int i = l; i <= r; ++i) {
//依次吧第i个数作为根的时候值
leftlist = backtruack(l, i - 1);
rightlist = backtruack(i + 1, r); //最后吧这两个值都组合起来
for (TreeNode lefttree : leftlist) {
for (TreeNode righttree : rightlist) {
TreeNode root = new TreeNode(i); //当前根节点
root.left = lefttree;
root.right = righttree;
res.add(root);
}
}
} return res;
}
}

【LEETCODE】68、动态规划,medium级别,题目:95、120、91的更多相关文章

  1. leetcode笔记 动态规划在字符串匹配中的应用

    目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...

  2. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  3. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  4. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  5. 【Leetcode】【Medium】Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  7. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  8. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  9. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

  10. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

随机推荐

  1. 第04组alpha冲刺(3/4)

    队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(3/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.收集各个组员的进度 2.写博客 展示GitHub当日代码/文档签入记录: 接下 ...

  2. python 函数注解 参数后面加冒号 函数后面加箭头

    由于 python 是动态语言,在定义函数时,参数是不需要指定类型的.当调用别人写的函数,而该函数有没有文档说明,只有通过看源代码才能知道需要传递什么类型的参数. 不过 python 提供了一种机制可 ...

  3. 声源定位之2精读《sound localization based on phase difference enhancement using deep neuarl networks》

    2.1.1 题目与摘要 1.为什么要增强IPD? The phase differences between the discrete Fourier transform (DFT) coeffici ...

  4. 类中嵌套定义指向自身的引用(C、C++、C#)或指针(C、C++)

    在定义类的时候,类中可以嵌套定义指向自身的引用(C.C++.C#)或指针(C.C++).详见代码: Node类: using System; using System.Collections.Gene ...

  5. HandlerMethodArgumentResolver的抽象實現AbstractNamedValueMethodArgumentResolver下的子类

    Annotation-based argument resolution 部分2 org.springframework.web.servlet.mvc.method.annotation.Reque ...

  6. python开源项目聚合推荐【1】

    ******************************************************* 01项目名:unimatrix 功能介绍:Python模拟“黑客帝国”影片中的终端动画脚 ...

  7. Java学习-056-Jsoup爬虫获取中国所有的三级行政区划数据(一)

    在涉及地址服务时,经常需要用到地址信息的获取,而行政区划可能不定期的发生变化,所以我们需要获取最新的行政区划信息.因行政区划数据量较大,Java中可以使用Jsoup进行数据的获取.处理. 大家经常用到 ...

  8. cheerio 服务器端的jquery

    cheerio https://cheerio.js.org/ Fast, flexible, and lean implementation of core jQuery designed spec ...

  9. 审阅模式中word保存不了

    word保存不了 觉得有用的话,欢迎一起讨论相互学习~Follow Me 昨天写论文出现一个怪事,发现自己word内容按ctrl+S 进行保存时可以的,但是当按X进行关闭时,出现 关闭不了,问我是否需 ...

  10. vc对话框程序运行时隐藏

    1.在资源编辑中设置对话框为不可见 2.在OnPaint中加入下面代码 void CMyDlg::OnPaint() { static b = false; if(!b){ ShowWindow(SW ...