0-1背包问题:https://blog.csdn.net/qq_40794973/article/details/102701052


416. 分割等和子集

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

/// 416. Partition Equal Subset Sum
/// https://leetcode.com/problems/partition-equal-subset-sum/description/
/// 记忆化搜索
/// 时间复杂度: O(len(nums) * O(sum(nums)))
/// 空间复杂度: O(len(nums) * O(sum(nums)))
public class Solution {// memo[i][c] 表示使用索引为[0...i]的这些元素,是否可以完全填充一个容量为c的背包// -1 表示为未计算; 0 表示不可以填充; 1 表示可以填充private int[][] memo;public boolean canPartition(int[] nums) {int sum = 0;for (int i = 0; i < nums.length; i++) {//if (nums[i] <= 0) {//    throw new IllegalArgumentException("numbers in nums must be greater than zero.");//}sum += nums[i];}if (sum % 2 == 1) {return false;}memo = new int[nums.length][sum / 2 + 1];for (int i = 0; i < nums.length; i++) {Arrays.fill(memo[i], -1);}return tryPartition(nums, nums.length - 1, sum / 2);}// 使用nums[0...index], 是否可以完全填充一个容量为sum的背包private boolean tryPartition(int[] nums, int index, int sum) {if (sum == 0) {return true;}if (sum < 0 || index < 0) {return false;}if (memo[index][sum] != -1) {return memo[index][sum] == 1;}memo[index][sum] = (tryPartition(nums, index - 1, sum) || tryPartition(nums, index - 1, sum - nums[index])) ? 1 : 0;return memo[index][sum] == 1;}//private static void printBool(boolean res) {//    System.out.println(res ? "True" : "False");//}//public static void main(String[] args) {//    int[] nums1 = {1, 5, 11, 5};//    printBool((new Solution()).canPartition(nums1));//    int[] nums2 = {1, 2, 3, 5};//    printBool((new Solution1()).canPartition(nums2));//}
}
/// 416. Partition Equal Subset Sum
/// https://leetcode.com/problems/partition-equal-subset-sum/description/
/// 动态规划
/// 时间复杂度: O(len(nums) * O(sum(nums)))
/// 空间复杂度: O(len(nums) * O(sum(nums)))
public class Solution {public boolean canPartition(int[] nums) {int sum = 0;for (int i = 0; i < nums.length; i++) {//if (nums[i] <= 0) {//    throw new IllegalArgumentException("numbers in nums must be greater than zero.");//}sum += nums[i];}if (sum % 2 == 1) {return false;}int n = nums.length;int C = sum / 2;boolean[] memo = new boolean[C + 1];for (int i = 0; i <= C; i++) {memo[i] = (nums[0] == i);}for (int i = 1; i < n; i++) {for (int j = C; j >= nums[i]; j--) {memo[j] = memo[j] || memo[j - nums[i]];}}return memo[C];}//private static void printBool(boolean res) {//    System.out.println(res ? "True" : "False");//}//public static void main(String[] args) {//    int[] nums1 = {1, 5, 11, 5};//    printBool((new Solution2()).canPartition(nums1));//    int[] nums2 = {1, 2, 3, 5};//    printBool((new Solution2()).canPartition(nums2));//}
}

322. 零钱兑换

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


https://pic.leetcode-cn.com/6c4cc96ea18a0736e0c3fc96337681c7fabc96ce51f32dd0d3b8eada948de7fd-file_1566557107843

/// Source : https://leetcode.com/problems/coin-change/solution/
/// Memory Search
/// Time Complexity: O(coins_size * amount)
/// Space Complexity: O(amount)
class Solution {//memo[X]总金额为X的时候,凑成总金额所需的最少的硬币个数private int[] memo;//标记private int maxAmount;public int coinChange(int[] coins, int amount) {//Integer.MAX_VALUE不可以,会发生越界maxAmount = amount + 1;memo = new int[amount + 1];Arrays.fill(memo, -1);int res = search(coins, amount);return res == maxAmount ? -1 : res;}private int search(int[] coins, int amount) {if (amount == 0) {return 0;}if (memo[amount] != -1) {return memo[amount];}int res = maxAmount;for (int i = 0; i < coins.length; i++) {if (amount >= coins[i]) {res = Math.min(res, 1 + search(coins, amount - coins[i]));}}return memo[amount] = res;}
}
/// Source : https://leetcode.com/problems/coin-change/solution/
/// Dynamic Problem
/// 0-1 backpack problem
///
/// Time Complexity: O(coins_size * amount)
/// Space Complexity: O(amount)
public class Solution {public int coinChange(int[] coins, int amount) {int[] dp = new int[amount + 1];Arrays.fill(dp, amount + 1);dp[0] = 0;for (int i = 0; i < coins.length; i++) {for (int j = coins[i]; j <= amount; j++) {dp[j] = Math.min(dp[j], 1 + dp[j - coins[i]]);}}return dp[amount] == amount + 1 ? -1 : dp[amount];}
}
/// Source : https://leetcode.com/problems/coin-change/solution/
/// Dynamic Problem
/// 0-1 backpack problem
///
/// Time Complexity: O(coins_size * amount)
/// Space Complexity: O(amount)
public class Solution {public int coinChange(int[] coins, int amount) {int[] dp = new int[amount + 1];Arrays.fill(dp, amount + 1);dp[0] = 0;for (int i = 1; i <= amount; i++) {for (int j = 0; j < coins.length; j++) {if (i - coins[j] >= 0) {dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);}}}return dp[amount] == amount + 1 ? -1 : dp[amount];}
}
//public static void main(String[] args) {
//    int[] coins = {1, 2, 5};
//    int amount = 11;
//    //3
//    System.out.println(new Solution().coinChange(coins, amount));
//    //------------------------------------------------------------------//
//    int[] coins02 = {2};
//    int amount02 = 3;
//    //-1
//    System.out.println(new Solution().coinChange(coins02, amount02));
//    //------------------------------------------------------------------//
//    int[] coins03 = {2, 5, 10, 1};
//    int amount03 = 27;
//    //4
//    System.out.println(new Solution().coinChange(coins03, amount03));
//    //-------------------------------------------------------------------//
//    int[] coins04 = {186, 419, 83, 408};
//    int amount04 = 6249;
//    //20
//    System.out.println(new Solution().coinChange(coins04, amount04));
//
//    int[] coins05 = {1};
//    int amount05 = 0;
//    //0
//    System.out.println(new Solution().coinChange(coins05, amount05));
//}

377. 组合总和 Ⅳ

https://leetcode-cn.com/problems/combination-sum-iv/


/// Source : https://leetcode.com/problems/combination-sum-iv/description/
/// Memory Search
/// Time Complexity: O(n * target)
/// Space Complexity: O(n * target)
class Solution {//memo[X]正整数为X的时候,组合的个数private int[] memeo;public int combinationSum4(int[] nums, int target) {if(nums.length == 0){return 0;}memeo = new int[target + 1];Arrays.fill(memeo, -1);//return solve(nums, target);solve(nums, target);return memeo[target];}private int solve(int[] nums, int target) {if (target == 0) {return 1;}if (memeo[target] != -1) {return memeo[target];}int res = 0;for (int i = 0; i < nums.length; i++) {if (target >= nums[i]) {res += solve(nums, target - nums[i]);}}return memeo[target] = res;}
}
/// Source : https://leetcode.com/problems/combination-sum-iv/description/
/// Dynamic Programming
/// Time Complexity: O(n * target)
/// Space Complexity: O(target)
class Solution {public int combinationSum4(int[] nums, int target) {int n = nums.length;if (n == 0) {return 0;}int[] memo = new int[target + 1];Arrays.fill(memo, 0);memo[0] = 1;for (int i = 1; i <= target; i++) {for (int j = 0; j < n; j++) {if (nums[j] <= i) {if (memo[i] == -1 || memo[i - nums[j]] == -1 || (long) memo[i] + (long) memo[i - nums[j]] > Integer.MAX_VALUE) {memo[i] = -1;} else {memo[i] += memo[i - nums[j]];}}}}//assert (memo[target] != -1);return memo[target];}
}
//public static void main(String[] args) {
//    int[] nums = {1, 2, 3};
//    int target = 4;
//    System.out.println(new Solution().combinationSum4(nums, target));
//}

474. 一和零

https://leetcode-cn.com/problems/ones-and-zeroes/


/// Source : https://leetcode.com/problems/ones-and-zeroes/description/
/// 0-1 backsack problem
/// Recursion Implimentation (Memory Search)
/// Time Complexity: O(sizeof(array)*m*n)
/// Space Complexity: O(sizeof(array)*m*n)
class Solution {public int findMaxForm(String[] strs, int m, int n) {//字符数组中对应下标0出现的频率int[] mcost = new int[strs.length];//Arrays.fill(mcost, 0);//字符数组中对应下标1出现的频率int[] ncost = new int[strs.length];//Arrays.fill(ncost, 0);for (int i = 0; i < strs.length; i++) {String str = strs[i];for (int j = 0; j < str.length(); j++) {if (str.charAt(j) == '0') {mcost[i]++;} else {ncost[i]++;}}}//在strs中第k个位置,有m个0,n个1,对应的能拼出存在于数组中的字符串的最大数量int[][][] dp = new int[strs.length][m + 1][n + 1];for (int i = 0; i < dp.length; i++) {for (int j = 0; j < dp[i].length; j++) {Arrays.fill(dp[i][j], -1);}}return findMaxForm(strs.length - 1, m, n, dp, mcost, ncost);}/*** @param k     当前操作的字符串* @param m     0的个数* @param n     1的个数* @param mcost 当前位置字符串中0的个数* @param ncost 当前位置字符串中1的个数*/private int findMaxForm(int k, int m, int n, int[][][] dp, int[] mcost, int[] ncost) {if (k < 0) {return 0;}if (dp[k][m][n] != -1) {return dp[k][m][n];}//不选这个字符串dp[k][m][n] = findMaxForm(k - 1, m, n, dp, mcost, ncost);//选这个字符串if (m >= mcost[k] && n >= ncost[k]) {dp[k][m][n] = Math.max(dp[k][m][n], 1 + findMaxForm(k - 1, m - mcost[k], n - ncost[k], dp, mcost, ncost));}return dp[k][m][n];}
}
/// Source : https://leetcode.com/problems/ones-and-zeroes/description/
/// 0-1 backsack problem
/// Dynamic Programming
/// Time Complexity: O(sizeof(array)*m*n)
/// Space Complexity: O(sizeof(array)*m*n)
class Solution {public int findMaxForm(String[] strs, int m, int n) {int[] mcost = new int[strs.length];//Arrays.fill(mcost, 0);int[] ncost = new int[strs.length];//Arrays.fill(ncost, 0);for (int i = 0; i < strs.length; i++) {String str = strs[i];for (int j = 0; j < str.length(); j++) {if (str.charAt(j) == '0') {mcost[i]++;} else {ncost[i]++;}}}int[][][] dp = new int[strs.length][m + 1][n + 1];//for (int i = 0; i < dp.length; i++) {//    for (int j = 0; j < dp[i].length; j++) {//        Arrays.fill(dp[i][j], 0);//    }//}for (int u = mcost[0]; u <= m; u++) {for (int v = ncost[0]; v <= n; v++) {dp[0][u][v] = 1;}}for (int i = 1; i < strs.length; i++) {for (int u = 0; u <= m; u++) {for (int v = 0; v <= n; v++) {dp[i][u][v] = dp[i - 1][u][v];if (u >= mcost[i] && v >= ncost[i]) {dp[i][u][v] = Math.max(dp[i][u][v], 1 + dp[i - 1][u - mcost[i]][v - ncost[i]]);}}}}return dp[strs.length - 1][m][n];}
}
/// Source : https://leetcode.com/problems/ones-and-zeroes/description/
/// Dynamic Programming with space optimization
/// Time Complexity: O(sizeof(array)*m*n)
/// Space Complexity: O(m*n)
class Solution {public int findMaxForm(String[] strs, int m, int n) {int[] mcost = new int[strs.length];//Arrays.fill(mcost, 0);int[] ncost = new int[strs.length];//Arrays.fill(ncost, 0);for (int i = 0; i < strs.length; i++) {String str = strs[i];for (int j = 0; j < str.length(); j++) {if (str.charAt(j) == '0') {mcost[i]++;} else {ncost[i]++;}}}int[][][] dp = new int[2][m + 1][n + 1];//for (int i = 0; i < dp.length; i++) {//    for (int j = 0; j < dp[i].length; j++) {//        Arrays.fill(dp[i][j], 0);//    }//}for (int u = mcost[0]; u <= m; u++) {for (int v = ncost[0]; v <= n; v++) {dp[0][u][v] = 1;}}for (int i = 1; i < strs.length; i++) {for (int u = 0; u <= m; u++) {for (int v = 0; v <= n; v++) {dp[i % 2][u][v] = dp[(i - 1) % 2][u][v];if (u >= mcost[i] && v >= ncost[i]) {dp[i % 2][u][v] = Math.max(dp[i % 2][u][v], 1 + dp[(i - 1) % 2][u - mcost[i]][v - ncost[i]]);}}}}return dp[(strs.length - 1) % 2][m][n];}
}
/// Source : https://leetcode.com/problems/ones-and-zeroes/description/
/// Dynamic Programming with space optimization
/// Time Complexity: O(sizeof(array)*m*n)
/// Space Complexity: O(m*n)
class Solution {public int findMaxForm(String[] strs, int m, int n) {int[][] dp = new int[m + 1][n + 1];//for (int i = 0; i < dp.length; i++) {//    Arrays.fill(dp[i], 0);//}for (int i = 0; i < strs.length; i++) {int mcost = 0, ncost = 0;String str = strs[i];for (int j = 0; j < str.length(); j++) {if (str.charAt(j) == '0') {mcost++;} else {ncost++;}}for (int u = m; u >= mcost; u--) {for (int v = n; v >= ncost; v--) {dp[u][v] = Math.max(dp[u][v], 1 + dp[u - mcost][v - ncost]);}}}return dp[m][n];}
}
//public static void main(String[] args) {
//    String[] array = {"10", "0001", "111001", "1", "0"};
//    int m = 5, n = 3;
//    //4
//    System.out.println(new Solution().findMaxForm(array, m, n));
//    //----------------------------------------------------------------//
//    String[] array02 = {"10", "0", "1"};
//    int m02 = 1, n02 = 1;
//    //2
//    System.out.println(new Solution().findMaxForm(array02, m02, n02));
//}

139. 单词拆分

https://leetcode-cn.com/problems/word-break/

class Solution {HashMap<String, Boolean> memeo;public boolean wordBreak(String s, List<String> wordDict) {if (s == null || s.length() == 0) {return false;}memeo = new HashMap<>();return isWordBreak(s, wordDict);}private boolean isWordBreak(String s, List<String> wordDict) {if (s.length() == 0) {return true;}if (memeo.containsKey(s)) {return memeo.get(s);}boolean flag = false;for (String word : wordDict) {if (s.startsWith(word)) {flag = (flag || isWordBreak(s.substring(word.length()), wordDict));}}memeo.put(s, flag);return flag;}
}
//private static List arrayToList(String[] wordArr){
//    List<String> res = new ArrayList<>();
//    res.addAll(Arrays.asList(wordArr));
//    return res;
//}//public static void main(String[] args) {
//    String s = "leetcode";
//    String[] wordArr = {"leet", "code"};
//    List wordDict = arrayToList(wordArr);
//    //true
//    System.out.println(new Solution().wordBreak(s, wordDict));
//    //------------------------------------------------------------------------//
//    String s02 = "applepenapple";
//    String[] wordArr02 = {"apple", "pen"};
//    List wordDict02 = arrayToList(wordArr02);
//    //true
//    System.out.println(new Solution().wordBreak(s02, wordDict02));
//    //------------------------------------------------------------------------//
//    String s03 = "catsandog";
//    String[] wordArr03 = {"cats", "dog", "sand", "and", "cat"};
//    List wordDict03 = arrayToList(wordArr03);
//    //false
//    System.out.println(new Solution().wordBreak(s03, wordDict03));
//    //------------------------------------------------------------------------//
//    String s04 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
//    String[] wordArr04 = {"a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"};
//    List wordDict04 = arrayToList(wordArr04) ;
//    //false
//    System.out.println(new Solution().wordBreak(s04, wordDict04));
//}

494. 目标和

https://leetcode-cn.com/problems/target-sum/


/// Source : https://leetcode.com/problems/target-sum/description/
/// Backtracking
/// Time Complexity: O(2^n)
/// Space Complexity: O(n)
class Solution {public int findTargetSumWays(int[] nums, int S) {return dfs(nums, 0, 0, S);}private int dfs(int[] nums, int index, int res, int S) {if (index == nums.length) {if (res == S) {return 1;} else {return 0;}}int ret = 0;ret += dfs(nums, index + 1, res - nums[index], S);ret += dfs(nums, index + 1, res + nums[index], S);return ret;}
}
/// Source : https://leetcode.com/problems/target-sum/description/
/// Backtracking
/// Using Stack - Non-recursion solution
///
/// Time Complexity: O(2^n)
/// Space Complexity: O(2^n)
class Solution {public int findTargetSumWays(int[] nums, int S) {ArrayDeque<Integer> indexStack = new ArrayDeque<>();ArrayDeque<Integer> sumStack = new ArrayDeque<>();indexStack.push(0);sumStack.push(0);int res = 0, index, sum;while (!indexStack.isEmpty()) {index = indexStack.pop();sum = sumStack.pop();if (index + 1 == nums.length) {if (sum + nums[index] == S) {res += 1;}if (sum - nums[index] == S) {res += 1;}} else {indexStack.push(index + 1);sumStack.push(sum + nums[index]);indexStack.push(index + 1);sumStack.push(sum - nums[index]);}}return res;}
}
/// Source : https://leetcode.com/problems/target-sum/description/
/// Memory Search
/// Using TreeSet
///
/// Time Complexity: O(n * maxNum * log(n * maxNum))
/// Space Complexity: O(n * maxNum)
import javafx.util.Pair;
class Solution {public int findTargetSumWays(int[] nums, int S) {Map<Pair<Integer, Integer>, Integer> dp = new HashMap<>();return dfs(nums, 0, S, dp);}private int dfs(int[] nums, int index, int S, Map<Pair<Integer, Integer>, Integer> dp) {if (index == nums.length) {if (S == 0) {return 1;} else {return 0;}}Pair<Integer, Integer> p = new Pair<>(index, S);if (dp.containsKey(p)) {return dp.get(p);}int ret = 0;ret += dfs(nums, index + 1, S - nums[index], dp);ret += dfs(nums, index + 1, S + nums[index], dp);dp.put(p, ret);return ret;}
}
/// Source : https://leetcode.com/problems/target-sum/description/
/// Memory Search
/// Using 2D-Array
///
/// Time Complexity: O(n * maxNum)
/// Space Complexity: O(n * maxNum)
class Solution {public int findTargetSumWays(int[] nums, int S) {int[][] dp = new int[nums.length][2001];for (int i = 0; i < nums.length; i++) {Arrays.fill(dp[i], -1);}return dfs(nums, 0, S, dp);}private int dfs(int[] nums, int index, int S, int[][] dp) {if (index == nums.length) {if (S == 0) {return 1;} else {return 0;}}if (S + 1000 < 0 || S + 1000 >= 2001) {return 0;}if (dp[index][S + 1000] != -1) {return dp[index][S + 1000];}int ret = 0;ret += dfs(nums, index + 1, S - nums[index], dp);ret += dfs(nums, index + 1, S + nums[index], dp);return dp[index][S + 1000] = ret;}
}
/// Source : https://leetcode.com/problems/target-sum/description/
/// Dynamic Programming
/// Using 2D-Array
///
/// Time Complexity: O(n * maxNum)
/// Space Complexity: O(n * maxNum)
class Solution {public int findTargetSumWays(int[] nums, int S) {int[][] dp = new int[nums.length][2001];//for (int i = 0; i < nums.length; i++) {//    Arrays.fill(dp[i], 0);//}dp[0][1000 - nums[0]] += 1;dp[0][1000 + nums[0]] += 1;for (int i = 1; i < nums.length; i++) {for (int j = 0; j < 2001; j++) {if (j - nums[i] >= 0 && j - nums[i] < 2001) {dp[i][j] += dp[i - 1][j - nums[i]];}if (j + nums[i] >= 0 && j + nums[i] < 2001) {dp[i][j] += dp[i - 1][j + nums[i]];}}}if (1000 + S < 0 || 1000 + S >= 2001) {return 0;}return dp[nums.length - 1][1000 + S];}
}
/// Source : https://leetcode.com/problems/target-sum/description/
/// Dynamic Programming
/// Using 1D Array
///
/// Time Complexity: O(n * maxNum)
/// Space Complexity: O(maxNum)
class Solution {public int findTargetSumWays(int[] nums, int S) {int[] dp = new int[2001];//Arrays.fill(dp, 0);dp[1000 - nums[0]] += 1;dp[1000 + nums[0]] += 1;for (int i = 1; i < nums.length; i++) {int[] a = new int[2001];//Arrays.fill(dp, 0);for (int j = 0; j < 2001; j++) {if (j - nums[i] >= 0 && j - nums[i] < 2001){a[j] += dp[j - nums[i]];}if (j + nums[i] >= 0 && j + nums[i] < 2001){a[j] += dp[j + nums[i]];}}dp = a;}if (1000 + S < 0 || 1000 + S >= 2001){return 0;}return dp[1000 + S];}
}

import javafx.util.Pair;
class Solution {//在nums数组中的第k个位置,目标数为S,目标数S的所有添加符号的方法数private Map<Pair<Integer, Integer>, Integer> memo = new HashMap<>();public int findTargetSumWays(int[] nums, int S) {return findTargetSumWays(nums, S, nums.length - 1, 0);}private int findTargetSumWays(int[] nums, int s, int index, int sum) {if (index == 0) {if (sum - nums[0] == s && sum + nums[0] == s) {return 2;} else if (sum - nums[0] == s && sum + nums[0] != s) {return 1;} else if (sum - nums[0] != s && sum + nums[0] == s) {return 1;}}if (index < 0) {return 0;}Pair<Integer, Integer> tempPair = new Pair<>(index, sum);if (memo.containsKey(tempPair)) {return memo.get(tempPair);}int count = 0;count += findTargetSumWays(nums, s, index - 1, sum - nums[index]);count += findTargetSumWays(nums, s, index - 1, sum + nums[index]);memo.put(tempPair, count);return count;}
}
//public static void main(String[] args) {
//    int[] nums = {1, 1, 1, 1, 1};
//    int s = 3;
//    System.out.println(new Solution().findTargetSumWays(nums, s));
//    //--------------------------------------------------------------------//
//    int[] nums02 = {0, 0, 0, 0, 0, 0, 0, 0, 1};
//    int s02 = 1;
//    //256
//    System.out.println(new Solution().findTargetSumWays(nums02, s02));
//    //Map<Pair<Integer, Integer>, Integer> testMap = new HashMap<>();
//    //testMap.put(new Pair<>(1,2), 666);
//    //System.out.println(testMap.containsKey(new Pair<>(1, 2)));
//}

416. 分割等和子集相关推荐

  1. LeetCode-动态规划背包题-416. 分割等和子集

    描述 416. 分割等和子集 给你一个 只包含正整数 的 非空 数组 nums .请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 示例 1: 输入:nums = [1,5,11, ...

  2. leetcode - 416. 分割等和子集

    416. 分割等和子集 -------------------------------------------- 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和 ...

  3. Suzy心情很差因为被charge了late fee Day42 | 动态规划之背包问题,416. 分割等和子集

    背包问题 01背包 Given n种物品,每种物品只有1个 每个物品有自己的重量.价值. 背包最大能装载的重量 动规五部曲--用二维数组 定义dp数组的含义:dp[ i ][ j ]表示[0,i]物品 ...

  4. Java实现 LeetCode 416 分割等和子集

    416. 分割等和子集 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 注意: 每个数组中的元素不会超过 100 数组的大小不会超过 200 示例 1: ...

  5. LeetCode 416 分割等和子集

    LeetCode 416 分割等和子集 题目链接 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 注意: 每个数组中的元素不会超过 100 数组的大小不会 ...

  6. 【Leetcode刷题】:Python:416. 分割等和子集

    题目 416. 分割等和子集 代码:dp class Solution:def canPartition(self, nums: List[int]) -> bool:n = len(nums) ...

  7. LeetCode 416. 分割等和子集 【c++/java详细题解】

    来自专栏<LeetCode高频面试题> 欢迎订阅 目录 1.题目 2.思路 3.二维c++代码 4.二维java代码 5.一维优化 6.一维c++代码 7.一维java代码 1.题目 给你 ...

  8. [416]. 分割等和子集

    [416]. 分割等和子集 题目 算法设计:动态规划 题目 题目:[416]. 分割等和子集 给你一个只包含正整数的非空数组 nums .请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和 ...

  9. day41 | 416. 分割等和子集

    01 背包问题 定义dp数组 dp[i][j]:i 表示 0 - i 之间的物品,任取,放进容量 为 j 的背包里 的最大价值 递推公式: dp[i][j] 由两个方向推出来,即放当前的物品i,与不放 ...

最新文章

  1. Spring框架深入(四)--SpringMVC配置详解
  2. 朱峰谈概念设计(五):进入焦距
  3. A Way to implement Abstract Class In Flex
  4. 如何在CDH5上部署Dolphin Scheduler 1.3.1
  5. Ext JS学习第二天 我们所熟悉的javascript(一)
  6. 【报告分享】2019中国少儿编程行业报告.pdf(附下载链接)
  7. 【软件工程】软件工程中应用的几种图辨析:系统流程图、数据流图、数据字典、实体联系图、状态转换图、层次方框图、Warnier图、IPO图、层次图、HIPO图、结构图、程序流程图、盒图、PAD图、判定表、
  8. nodejs学习笔记(3)
  9. 不要变得迟钝,努努力,什么都迎刃而解
  10. 最新Latex安装详细教程
  11. 计算机中丢失msvcr71.dll 问题解决
  12. 牛牛的汉诺塔(记忆化搜索)
  13. Qsys中的EPCS使用技巧
  14. 后端-科室信息管理接口
  15. collection接口
  16. 在MTK7628平台编译Silicon的zigbee Host程序
  17. 一体的综合化云控平台
  18. JS面试题汇总(Es6)
  19. 鱼雷导引仿真matlab
  20. rtf富文本_轻松生成动态RTF(富文本格式)文档

热门文章

  1. 树莓派基于PS2操纵杆的飞机大战小游戏
  2. Unity 使用ContentSizeFitter刷新不及时的问题
  3. 【2021情人节主题征文】| 写了一个表白网页后,我跟女神在一起啦
  4. java课程设计计算器 uml简图,计算器的用例建模
  5. Android系统SD卡各类文件夹名称
  6. 记录下我磕磕碰碰的三个月找工作经历,offer拿到手软
  7. mysql 1593_MySQL数据库经典错误三 Last_IO_Errno: 1593(server-id冲突)
  8. 小红书SEO之关键词排名优化详解【从入门到精通】
  9. 网站关键词排名优化怎么做才能让排名更高?
  10. echarts 旭日图sunburst