import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param weights int整型一维数组 * @return bool布尔型 */ public boolean fang(int index,int res,int []weights,int sum){ if(index >= weights.length) return false; sum += weights[index]; if(sum == res) return true; if(fang(index+1,res,weights,sum)){ return true; } if(fang(index+1,res,weights,sum - weights[index])){ return true; } return false; } public boolean canPartition (int[] weights) { // write code here int sum = 0; for(int i = 0;i<weights.length;i++){ sum+=weights[i]; } int res = sum/2; if(res * 2 != sum){ return false; } return fang(0,res,weights,0); } }