Leetcode 416. Partition Equal Subset Sum
2017-02-09 16:37
344 查看
子集和问题
更常见的亦可扩展为背包问题:
0-1 背包问题:
dp[0] = true;
dp[0] = 1;
for(int n: nums){
for(int h=S;h>=n;h--){
dp[h] = dp[h]||dp[h-n];
//dp[h] = dp[h]+dp[h-n]; // roll array.
}
}
更加普遍的背包问题(忘了术语是啥):
dp[0] = true;
dp[0] = 1;
for(int n: nums){
for(int h=0;h>=n,h<=S;h++){
dp[h] = dp[h]||dp[h-n];
//dp[h] = dp[h]+dp[h-n]; // roll array.
}
}
题目链接: Leetcode 416. Partition Equal Subset Sum
典型动态规划问题,下面是采用dp[]数组表示有多少种子集和计数方案
但是出现如下错误:
将 dp[] 数组改为boolean类型时:有无子集和出现时,代码为下:
Accepted!
I donot know why?
原因在于整型值溢出了
更常见的亦可扩展为背包问题:
0-1 背包问题:
dp[0] = true;
dp[0] = 1;
for(int n: nums){
for(int h=S;h>=n;h--){
dp[h] = dp[h]||dp[h-n];
//dp[h] = dp[h]+dp[h-n]; // roll array.
}
}
更加普遍的背包问题(忘了术语是啥):
dp[0] = true;
dp[0] = 1;
for(int n: nums){
for(int h=0;h>=n,h<=S;h++){
dp[h] = dp[h]||dp[h-n];
//dp[h] = dp[h]+dp[h-n]; // roll array.
}
}
题目链接: Leetcode 416. Partition Equal Subset Sum
典型动态规划问题,下面是采用dp[]数组表示有多少种子集和计数方案
public class Solution { public boolean canPartition(int[] nums) { int sum = 0; for(int i=0;i<nums.length;i++){ sum += nums[i]; } if(sum%2==1){ return false; } return subsumSet(nums,sum/2); //return ((sum%2)==1)?false:subsumSet(nums,sum/2); } private boolean subsumSet(int[] nums, int S) { // TODO Auto-generated method stub // out //boolean[] dp = new boolean[S+1]; int[] dp = new int[S+1]; //dp[0] = true; dp[0] = 1; for(int n: nums){ for(int h=S;h>=n;h--){ //dp[h] = dp[h]||dp[h-n]; dp[h] = dp[h]+dp[h-n]; // roll array. } } //return dp[S]; ` if(dp[S]>=1) return true; else{ return false; } /* if(i==0){ if(nums[i]==S) return true; else return false; } // dynamic programming return subsumSet(nums,S,i-1)||subsumSet(nums,S-nums[i],i-1); */ } }
但是出现如下错误:
将 dp[] 数组改为boolean类型时:有无子集和出现时,代码为下:
public class Solution { public boolean canPartition(int[] nums) { int sum = 0; for(int i=0;i<nums.length;i++){ sum += nums[i]; } if(sum%2==1){ return false; } return subsumSet(nums,sum/2); //return ((sum%2)==1)?false:subsumSet(nums,sum/2); } private boolean subsumSet(int[] nums, int S) { // TODO Auto-generated method stub // out //boolean[] dp = new boolean[S+1]; //int[] dp = new int[S+1]; dp[0] = true; //dp[0] = 1; for(int n: nums){ for(int h=S;h>=n;h--){ dp[h] = dp[h]||dp[h-n]; //dp[h] = dp[h]+dp[h-n]; // roll array. } } return dp[S]; ` /* if(dp[S]>=1) return true; else{ return false; } */ /* if(i==0){ if(nums[i]==S) return true; else return false; } // dynamic programming return subsumSet(nums,S,i-1)||subsumSet(nums,S-nums[i],i-1); */ } }
Accepted!
I donot know why?
原因在于整型值溢出了
相关文章推荐
- 【leetcode】416. Partition Equal Subset Sum
- Leetcode 416. Partition Equal Subset Sum[medium]
- Leetcode解题笔记 416. Partition Equal Subset Sum [Medium] 动态规划
- [LeetCode]416. Partition Equal Subset Sum
- LeetCode [416. Partition Equal Subset Sum] 难度[medium]
- Leetcode 416. Partition Equal Subset Sum 对半分 解题报告【修正版】
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
- LeetCode No.416 Partition Equal Subset Sum
- 416. Partition Equal Subset Sum
- leetCode No.416 Partition Equal Subset Sum
- LeetCode: Partition Equal Subset Sum
- 每周LeetCode算法题(五):416. Partition Equal Subset Sum
- [LeetCode] 416 Partition Equal Subset Sum
- LeetCode[416] Partition Equal Subset Sum
- Leetcode Algorithm 416. Partition Equal Subset Sum
- [LeetCode]Partition Equal Subset Sum
- (M)Dynamic Programming:416. Partition Equal Subset Sum
- [LeetCode416]Partition Equal Subset Sum
- leetcode--- Partition Equal Subset Sum---动规
- 416. Partition Equal Subset Sum 题解