动态规划

Posted 保护眼睛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划相关的知识,希望对你有一定的参考价值。

求两个字符串的最大公共长度

    public static int getMax(String s1, String s2) {
        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();

        int len1 = s1.length();
        int len2 = s2.length();

        int[][] res = new int[len1 + 1][len2 + 1];
        int MaxRes = 0;

        for (int i = 1; i < res.length; i++) {
            for (int j = 1; j < res[0].length; j++) {

                if (chars1[i - 1] == chars2[j - 1]) {
                    res[i][j] = res[i - 1][j - 1] + 1;
                }

                MaxRes = Math.max(res[i][j], MaxRes);
            }
        }

        return MaxRes;
    }

119.杨辉三角II

    public List<Integer> getRow(int rowIndex) {
        List<Integer> res = new ArrayList<>(rowIndex + 1);
        long pre = 1;

        for (int i = 0; i <= rowIndex; i++) {
            res.add((int)pre);
            pre = pre * (rowIndex - i) / (i + 1);
        }

        return res;
    }

55.跳跃游戏

    public boolean canJump(int[] nums) {
        int MaxReach = 0;
        int len = nums.length;

        for (int i = 0; i < len; i++) {
            if (MaxReach < i) {
                return false;
            }
            MaxReach = Math.max(MaxReach, i + nums[i]);
        }

        return true;
    }

62.不同的路径

    public int uniquePaths(int m, int n) {
        int[][] res = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && j == 0) {
                    res[0][0] = 1;
                } else if (i == 0 && j != 0) {
                    res[i][j] = res[i][j - 1];
                } else if (j == 0 && i != 0) {
                    res[i][j] = res[i - 1][j];
                } else {
                    res[i][j] = res[i - 1][j] + res[i][j - 1];
                }
            }
        }

        return res[m - 1][n - 1];
    }

45.跳跃游戏II

    public static int jump(int[] nums) {
        int len = nums.length;
        int[] res = new int[len];
        res[0] = 0;

        for (int i = 1; i < len; i++) {
            res[i] = nums.length;
        }

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[j] + j >= i) {
                    res[i] = Math.min(res[j] + 1, res[i]);
                }
            }
        }

        return res[len - 1];
    }

    public static int jump1(int[] nums) {
        int len = nums.length;
        int[] res = new int[len];
        res[0] = 0;

        for (int i = 1; i < len; i++) {
            res[i] = nums.length;
        }

        for (int i = 0; i < len; i++) {
            for (int j = 1; j <= nums[i]; j++) {
                if (i + j >= len) {
                    return res[len - 1];
                }
                res[i + j] = Math.min(res[i] + 1, res[i + j]);
            }
        }

        return res[len - 1];
    }

96.不同的二叉搜索树

      public static int numTrees(int n) {
      int[] res = new int[n + 1];
      res[0] = 1;
      res[1] = 1;

      for (int i = 2; i < res.length; i++) {
          for (int j = 1; j <= i; j++) {
              res[i] += res[j - 1] * res[i - j];
          }
      }

      return res[n];
  }

338.比特位计数

    public static int[] countBits(int n) {
        int[] res = new int[n + 1];

        for (int i = 0; i <= n; i++) {
            res[i] = getCount(i);
        }
        return res;
    }

    public static int getCount(int num) {
        int count = 0;

        while (num != 0) {
            if ((num & 1) == 1) {
                count++;
            }
            num = num >>> 1;
        }
        return count;
    }

    public static int[] countBits2(int n) {
        int[] res = new int[n + 1];

        for (int i = 0; i <= n; i++) {
            res[i] = res[i >> 1] + (i & 1);
        }
        return res;
    }

以上是关于动态规划的主要内容,如果未能解决你的问题,请参考以下文章

是否可以动态编译和执行 C# 代码片段?

动态规划_线性动态规划,区间动态规划

应对笔试手写代码,如何准备动态规划?

应对笔试手写代码,如何准备动态规划?

应对笔试手写代码,如何准备动态规划?

算法动态规划 ⑤ ( LeetCode 63.不同路径 II | 问题分析 | 动态规划算法设计 | 代码示例 )