LeetCode-动态规划

一个人想着一个人 提交于 2019-11-30 15:14:53

72. Edit Distance 最短编辑距离

https://leetcode.com/problems/edit-distance/

题目:给定两个单词Word 1和Word2,找到将word 1转换为Word2所需的最小操作数。允许对一个单词执行以下3种操作:插入字符、删除字符、替换字符。

思路:

class Solution {
    public int minDistance(String word1, String word2) {
        int a = word1.length();
        int b = word2.length();
        int[][] dp = new int[b+1][a+1];
        dp[0][0] = 0;
        for(int i = 1; i < a+1; i++) {
            dp[0][i] = i;
        }
        for(int i = 1; i < b+1; i++) {
            dp[i][0] = i;
        }
        for(int j = 1; j < b+1; j++) {
            for(int i = 1; i < a+1; i++) {
                if(word1.charAt(i-1) == word2.charAt(j-1)){
                    dp[j][i] = dp[j-1][i-1];
                } else {
                    int temp = Math.min(dp[j-1][i], dp[j-1][i-1]);
                    dp[j][i] = Math.min(temp, dp[j][i-1]) + 1;
                }
            }
        }
        return dp[b][a];
    }
}

322. Coin Change 换硬币

https://leetcode.com/problems/coin-change/

题目:你会得到不同面额的硬币和总金额。编写一个函数来计算弥补这个数量所需的最少硬币数。如果这个数额的钱不能由任何组合的硬币,返回-1。

思路:

class Solution {
    public int coinChange(int[] coins, int amount) {
        int n = coins.length;
        int[] dp = new int[amount+1];
        for(int i = 1; i < amount+1; i++) {
            dp[i] = amount+1;
        }
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            int temp = i;
            int cur = 0;
            for(int j = 0; j < n; j++) {
                if(coins[j] > temp) continue;
                cur = 1 + dp[temp-coins[j]];
                dp[i] = Math.min(dp[i], cur);
            }
        }
        if(dp[amount] < amount+1) {
            return dp[amount];
        } else {
            return -1;
        }
    }
}

416. Partition Equal Subset Sum 分成和相等的两个子集

https://leetcode.com/problems/partition-equal-subset-sum/

题目:给定一个只包含正整数的非空数组,请查找该数组是否可以划分为两个子集,以便两个子集中的元素之和相等。

思路:

class Solution {
    public boolean canPartition(int[] nums) {
        int n = nums.length;
        int total = 0, target = 0;
        for(int i = 0; i < n; i++) {
            total += nums[i];
        }
        if(total % 2 == 1) return false;
        target = total / 2;
        boolean[] dp = new boolean[target+1];
        for(int i = 0; i < target+1; i++) {
            dp[i] = false;
        }
        dp[0] = true;
        for(int i = 0; i < n; i++) {
            for(int j = target; j >= nums[i]; j--) {
                dp[j] = dp[j] | dp[j-nums[i]];
            }
        }
        return dp[target];
    }
}

771. Jewels and Stones 宝石和石头

https://leetcode.com/problems/jewels-and-stones/

题目:字符串J代表宝石的类型,字符串S代表你拥有的石头。S中的每一个字符都是你所拥有的一种石头。你想知道你有多少石头属于宝石。J中的字母是独立的,而J和S中的所有字符都是字母。字母区分大小写,因此“a”被认为是一种不同于“A”的石头。

思路:

class Solution {
    public int numJewelsInStones(String J, String S) {
        int jl = J.length();
        int sl = S.length();
        int[][] dp = new int[jl+1][sl+1];
        for(int i = 0; i < jl+1; i++) {
            dp[i][0] = 0;
        }
        for(int i = 0; i < sl+1; i++) {
            dp[0][i] = 0;
        }
        for(int i = 1; i < jl+1; i++) {
            int temp = 0;
            for(int j = 1; j < sl+1; j++) {
                if(S.charAt(j-1)==J.charAt(i-1)) temp++;
                dp[i][j] = temp + dp[i-1][j];
            }
        }
        return dp[jl][sl];
    }
}

动态规划:
http://oj.leetcode.com/problems/triangle/ (最短路径)
http://oj.leetcode.com/problems/subsets/ (另一种形式)
http://oj.leetcode.com/problems/subsets-ii/
// http://oj.leetcode.com/problems/edit-distance/ (经典)
http://oj.leetcode.com/problems/word-break/
http://oj.leetcode.com/problems/word-break-ii/
http://oj.leetcode.com/problems/unique-binary-search-trees/ (动态规划避免递归)
http://oj.leetcode.com/problems/unique-paths-ii/
http://oj.leetcode.com/problems/scramble-string/
http://oj.leetcode.com/problems/palindrome-partitioning/
http://oj.leetcode.com/problems/palindrome-partitioning-ii/
http://oj.leetcode.com/problems/interleaving-string/
http://oj.leetcode.com/problems/distinct-subsequences/
http://oj.leetcode.com/problems/decode-ways/
http://oj.leetcode.com/problems/gray-code/
http://oj.leetcode.com/problems/minimum-path-sum/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!