Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数组中的int元素分成2个相等或几乎相等的suma_Java - Fatal编程技术网

Java 将数组中的int元素分成2个相等或几乎相等的suma

Java 将数组中的int元素分成2个相等或几乎相等的suma,java,Java,问题如下: 我们有一个整数数组。。。我们需要检查是否可以将其划分为两个数组,其中两个数组的元素之和相同。。。如果总数不能相等,那么我们将数组分成2个部分,其中总数尽可能相等/接近 这是它检查是否可以用相等的和进行分区的部分: public boolean canPartition(int[] num) { int sum = 0; for (int i = 0; i < num.length; i++) sum += num[i];

问题如下: 我们有一个整数数组。。。我们需要检查是否可以将其划分为两个数组,其中两个数组的元素之和相同。。。如果总数不能相等,那么我们将数组分成2个部分,其中总数尽可能相等/接近

这是它检查是否可以用相等的和进行分区的部分:

public boolean canPartition(int[] num) {
        int sum = 0;
        for (int i = 0; i < num.length; i++)
            sum += num[i];

        // if 'sum' is a an odd number, we can't have two subsets with equal sum
        if(sum % 2 != 0)
            return false;

        return this.canPartitionRecursive(num, sum/2, 0);
    }

    private boolean canPartitionRecursive(int[] num, int sum, int currentIndex) {
        // base check
        if (sum == 0)
            return true;

        if(num.length == 0 || currentIndex >= num.length)
            return false;

        // recursive call after choosing the number at the currentIndex
        // if the number at currentIndex exceeds the sum, we shouldn't process this
        if( num[currentIndex] <= sum ) {
            if(canPartitionRecursive(num, sum - num[currentIndex], currentIndex + 1))
                return true;
        }

        // recursive call after excluding the number at the currentIndex
        return canPartitionRecursive(num, sum, currentIndex + 1);
    }
    
public boolean canPartition(int[]num){
整数和=0;
for(int i=0;i=num.length)
返回false;
//在currentIndex中选择数字后的递归调用
//如果currentIndex处的数字超过总和,则不应处理此问题
如果(num[currentIndex]=0;j--){
if(sum1

这个解决方案似乎不能处理所有输入,我也不明白为什么……你能帮我吗?

这不是一个布尔问题,近似的解决方案也可以

  • 这个问题的意思是计算一半的和:一半。(总和可能是奇数,所以是整数的一半。)

  • 然后用sum为第一个数组拾取元素这不是一个布尔问题,近似的解决方案也会这样做

    • 这个问题的意思是计算一半的和:一半。(总和可能是奇数,所以是整数的一半。)
    • 然后为第一个数组选择元素,并使用和
              int sum1 = 0;
              int sum2 = 0;
              for (int j = numOfItems - 1; j >= 0; j--) {
      
                  if (sum1 < sum2) {
                      sum1 += itemsWeight[j];
                  } else {
                      sum2 += itemsWeight[j];
                  }
                 
              }