1. package y2019.Algorithm.dynamicprogramming.medium;
  2.  
  3. /**
  4. * @ProjectName: cutter-point
  5. * @Package: y2019.Algorithm.dynamicprogramming.medium
  6. * @ClassName: NumDecodings
  7. * @Author: xiaof
  8. * @Description: 91. Decode Ways
  9. * A message containing letters from A-Z is being encoded to numbers using the following mapping:
  10. *
  11. * 'A' -> 1
  12. * 'B' -> 2
  13. * ...
  14. * 'Z' -> 26
  15. * Given a non-empty string containing only digits, determine the total number of ways to decode it.
  16. *
  17. * Example 1:
  18. *
  19. * Input: "12"
  20. * Output: 2
  21. * Explanation: It could be decoded as "AB" (1 2) or "L" (12).
  22. * Example 2:
  23. *
  24. * Input: "226"
  25. * Output: 3
  26. * Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
  27. * @Date: 2019/8/15 8:54
  28. * @Version: 1.0
  29. */
  30. public class NumDecodings {
  31.  
  32. public int solution(String s) {
  33. if (s == null || s.equals("") || (s.length() == 1 && s.equals("0"))) {
  34. return 0;
  35. }
  36. //我们发现这个串总最多一次可以使用2个字符,并且这两个字符组成的数要小于26或等于26才行
  37. //那么我们可以发先要获取当前字符能组成的解码数可分为
  38. //dp[n] = dp[n-1] + {if(2num <= 26}{dp[n-2} 只有满足使用最后2位数作为解码数字的时候才能加上不用这个2个字符可以组成的个数
  39. int[] dp = new int[s.length() + 1];
  40. dp[0] = 1;dp[1] = 1;
  41. char[] sc = s.toCharArray();
  42.  
  43. for (int i = 2; i < dp.length; ++i) {
  44. //i用来标识取s的前i个字符,还要判断这个字符不能是0,不然不能单个字符使用
  45. if (sc[i - 1] != '0') {
  46.  
  47. dp[i] = dp[i - 1];
  48. }
  49. //判断如果去除2个数的位置
  50. int l = i - 2;
  51. int value = Integer.valueOf(s.substring(l, i));
  52. if (value <= 26 && value > 0) {
  53. dp[i] += dp[i - 2];
  54. }
  55. }
  56. return dp[s.length()];
  57. }
  58.  
  59. public static void main(String[] args) {
  60. String s = "12";
  61. String s2 = "226";
  62. String s3 = "10";
  63.  
  64. NumDecodings fuc = new NumDecodings();
  65.  
  66. fuc.solution(s3);
  67.  
  68. }
  69. }
  1. package y2019.Algorithm.dynamicprogramming.medium;
  2.  
  3. import java.util.List;
  4.  
  5. /**
  6. * @ProjectName: cutter-point
  7. * @Package: y2019.Algorithm.dynamicprogramming.medium
  8. * @ClassName: MinimumTotal
  9. * @Author: xiaof
  10. * @Description: 120. Triangle
  11. * Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
  12. *
  13. * For example, given the following triangle
  14. *
  15. * [
  16. * [2],
  17. * [3,4],
  18. * [6,5,7],
  19. * [4,1,8,3]
  20. * ]
  21. * The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
  22. * @Date: 2019/8/15 8:54
  23. * @Version: 1.0
  24. */
  25. public class MinimumTotal {
  26.  
  27. public int solution(List<List<Integer>> triangle) {
  28. //这题我们反向遍历,从底往上进行遍历最小值
  29. //可以得知第k行第i个数的最小路径是minpath[k][i]=min{minpath[k+1][i], minpath[k+1][i+1]} + triangle[k][i]
  30. int[][] minpath = new int[triangle.size() + 1][triangle.size() + 1];
  31. for (int row = triangle.size() - 1; row >= 0; --row) {
  32. //列遍历数据
  33. for (int column = 0; column < triangle.get(row).size(); ++column) {
  34. minpath[row][column] = Math.min(minpath[row + 1][column], minpath[row + 1][column + 1]) + triangle.get(row).get(column);
  35. }
  36. }
  37.  
  38. return minpath[0][0];
  39. }
  40. }
  1. package y2019.Algorithm.dynamicprogramming.medium;
  2.  
  3. import y2019.Algorithm.common.TreeNode;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. /**
  9. * @ProjectName: cutter-point
  10. * @Package: y2019.Algorithm.dynamicprogramming.medium
  11. * @ClassName: GenerateTrees
  12. * @Author: xiaof
  13. * @Description: 95. Unique Binary Search Trees II
  14. *
  15. * Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
  16. *
  17. * Example:
  18. *
  19. * Input: 3
  20. * Output:
  21. * [
  22. * [1,null,3,2],
  23. * [3,2,null,1],
  24. * [3,1,null,null,2],
  25. * [2,1,3],
  26. * [1,null,2,null,3]
  27. * ]
  28. * Explanation:
  29. * The above output corresponds to the 5 unique BST's shown below:
  30. *
  31. * 1 3 3 2 1
  32. * \ / / / \ \
  33. * 3 2 1 1 3 2
  34. * / / \ \
  35. * 2 1 2 3
  36. *
  37. * @Date: 2019/8/15 8:54
  38. * @Version: 1.0
  39. */
  40. public class GenerateTrees {
  41.  
  42. public List<TreeNode> solution(int n) {
  43.  
  44. if (n == 0) return new ArrayList<>();
  45.  
  46. return backtruack(1, n);
  47. }
  48.  
  49. //因为要求出所有可能性,那么需要递归出所有结果
  50. public List<TreeNode> backtruack(int l, int r) {
  51. //我们需要进行操作的范围是l->r
  52. List<TreeNode> res = new ArrayList<>();
  53. //如果l>r超过了,那么直接返回一个空的集合,因为这个区间不可能组成一颗树
  54. if (l > r) {
  55. res.add(null);
  56. return res;
  57. }
  58. //如果l == r 那么就返回以当前节点作为根的树
  59. if (l == r) {
  60. res.add(new TreeNode(l));
  61. return res;
  62. }
  63. //其余情况把l->r的所有节点进行遍历,依次作为根节点进行组合
  64. List<TreeNode> leftlist, rightlist;
  65. for (int i = l; i <= r; ++i) {
  66. //依次吧第i个数作为根的时候值
  67. leftlist = backtruack(l, i - 1);
  68. rightlist = backtruack(i + 1, r);
  69.  
  70. //最后吧这两个值都组合起来
  71. for (TreeNode lefttree : leftlist) {
  72. for (TreeNode righttree : rightlist) {
  73. TreeNode root = new TreeNode(i); //当前根节点
  74. root.left = lefttree;
  75. root.right = righttree;
  76. res.add(root);
  77. }
  78. }
  79. }
  80.  
  81. return res;
  82. }
  83. }

【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. loj2058 「TJOI / HEOI2016」求和 NTT

    loj2058 「TJOI / HEOI2016」求和 NTT 链接 loj 思路 \[S(i,j)=\frac{1}{j!}\sum\limits_{k=0}^{j}(-1)^{k}C_{j}^{k ...

  2. Gaussian Processes

    原文地址:https://borgwang.github.io/ml/2019/07/28/gaussian-processes.html 一元高斯分布 概率密度函数:\[p(x) = \frac{1 ...

  3. SpringBoot——配置文件占位符

    在配置文件中采用: ${random.int} 获取一个随机值.

  4. 【传输协议】thrift的IDL语法

    一.IDL Thrift 采用IDL(Interface Definition Language)来定义通用的服务接口,然后通过Thrift提供的编译器,可以将服务接口编译成不同语言编写的代码,通过这 ...

  5. Hash算法及java HashMap底层实现原理理解(含jdk 1.7以及jdk 1.8)

    现在很多公司面试都喜欢问java的HashMap原理,特在此整理相关原理及实现,主要还是因为很多开发集合框架都不甚理解,更不要说各种其他数据结构了,所以造成面子造飞机,进去拧螺丝. 1.哈希表结构的优 ...

  6. tp的ajaxReturn后, 还要用echo $rt吗?

    首先你要看 ajaxReturn的原型: protected function ajaxReturn ($data, $type='', $json_option=0){ ........ switc ...

  7. WebRTC搭建前端视频聊天室——信令篇

    这篇文章讲述了WebRTC中所涉及的信令交换以及聊天室中的信令交换,主要内容来自WebRTC in the real world: STUN, TURN and signaling,我在这里提取出的一 ...

  8. 【GMT43智能液晶模块】例程十五:LAN_TCPC实验——以太网数据传输

    源代码下载链接: 链接:https://pan.baidu.com/s/1bFX8_UpUlML29oqoDGaw5g提取码:mrf5 复制这段内容后打开百度网盘手机App,操作更方便哦 GMT43购 ...

  9. LeetCode 101. Symmetric Tree(镜像树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  10. Tensorflow不能使用GPU的解决办法

    转载:https://blog.csdn.net/kudou1994/article/details/86735451 服务器在训练模型,另一边我在瞎胡乱搞不晓得咋个搞的,就不能使用GPU了.pyth ...