给定一个只包含正整数的数组 nums ,请问能否把这个数组取出若干个数使得取出的数之和和剩下的数之和相同。
数据范围: , 数组中的元素满足
private boolean recurrent(int[] nums, int depth, int rest) { if(rest < 0) return false; if(rest == 0) return true; // base case for(int i = depth; i < nums.length; i++){ if(recurrent(nums, i, rest - nums[i])) return true; } return false; }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return bool布尔型 */ public boolean partition (int[] nums) { // write code here int sum = 0; for(int i = 0; i < nums.length; i++){ sum += nums[i]; } // 无法均分 if(sum % 2 != 0){ return false; } int target = sum / 2; boolean[][] dp = new boolean[nums.length][target + 1]; for(int i = 0; i < nums.length; i++){ dp[i][0] = true; // base case } for(int i = 0; i < nums.length; i++){ for(int rest = nums[i]; rest <= target; rest++){ dp[i][rest] |= dp[i][rest - nums[i]]; } } return dp[0][target]; } }
暴力递归:和为奇数肯定不能满足。和为偶数再递归查找。
import java.util.*; public class Solution { public boolean partition (int[] nums) { int sum = 0; for(int i = 0; i < nums.length; ++i) { sum += nums[i]; } if(sum % 2 == 1) return false; return func(sum / 2, nums, 0, 0); } public boolean func(int target, int[] nums, int index, int cur) { if(cur > target) return false; if(cur == target) return true; if(index >= nums.length) return false; return func(target, nums, index + 1, cur + nums[index]) || func(target, nums, index + 1, cur); } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return bool布尔型 */ bool partition(vector<int>& nums) { // write code here int sum_nums = 0; for (int i : nums) { sum_nums += i; } if (sum_nums % 2 != 0) return false; sum_nums = sum_nums / 2; vector<int> dp(sum_nums+1, 0); for (int i = 0; i < nums.size(); i++) { for (int j = sum_nums; j >= 1; j--) { if (j - nums[i] >= 0) dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]); } } return dp[sum_nums] == sum_nums; } };
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return bool布尔型 */ public boolean partition (int[] nums) { // write code here int sum = 0; for(int i = 0; i < nums.length; i++){ sum += nums[i]; } if(sum % 2 != 0){ return false; } int n = sum / 2; int[][] dp = new int[nums.length][n + 1]; for(int i = 1; i < nums.length; i++){ for(int j = 1; j <= n; j++){ // 可以放 if(j >= nums[i]){ dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); }else{ dp[i][j] = dp[i - 1][j]; } } } return dp[nums.length - 1][n] == n; } }
class Solution: def partition(self , nums: List[int]) -> bool: '''找重叠子问题''' '''不能只靠脑子想''' target = sum(nums) if target % 2 != 0: return False dp = [[0 for line in range(target//2 + 1)] for row in range(len(nums))] for i in range (1,target//2 + 1): if i < nums[0] : dp[0][i] = dp[0][i-1] else : dp[0][i] = nums[0] for i in range(1,len(nums)): for j in range(1, target//2 + 1): if j < nums[i] : dp[i][j] = dp[i-1][j] else: dp[i][j] = max(dp[i-1][j], nums[i] + dp[i-1][j-nums[i]]) if dp[len(nums)-1][target//2] == target//2: return True else : return False 但是会超时 ,没有做优化。