繁体   English   中英

给定一个包含(-100 到 100)之间的 N 个整数的数组 A Java

[英]Given an array A of N integers between(-100 and 100) Java

我正在练习 Java,我的教授给了我这个问题:

给定一个包含 N 个整数(介于 -100 和 100 之间)的数组 A,计算数组内所有元素的相乘值并根据 output 返回 (-1, 0, 1)。

例如:

A[1,2,3] will return 1 because the output is (6), 
A[-1,2,3] will return -1 because the output is (-6), 
A[1,2,0] will return 0 because the output is (0)

方法定义:

public static int solution(int[] A) {
}

我的方法:我认为这将是一个递归过程,因为我将有多个输入。

处理一个块 A[1,2,3],将值 (1) 存储到 newArr[],处理另一个块 A[-1,2,3],将值 (-1) 存储到 newArr[] 等等on... 最后返回 newArr[] ,其中包含正确的值。

到目前为止我所拥有的,现在我有点卡住了..

public static void main(String[] args) {
        // TODO Auto-generated method stub
         int arr[] = {6,-7,8,9};
         int arr1[] = {-6,-7,8,9};
         int arr2[] = {0,-7,8,9};
         System.out.println("[ " + solution(arr) + " ]  => Needs to be -1" );
         System.out.println("[ " + solution(arr1) + " ] => Needs to be  1");
         System.out.println("[ " + solution(arr2) + " ] => Needs to be 0");
         System.out.println("Final return should be (-1, 1, 0)");
    }
    
    public static int solution(int[] A) {
        int temp = 1;
        for (int i = 0; i < A.length; i++){
            temp *= A[i];
        }
        return temp;
    }

我不知道这是否是正确的方法,关于我应该如何 go 这样做的任何想法?

提前致谢!

无需进行任何乘法运算。

  • 如果任何数字为0 ,则立即返回0 ,因为乘积为零。
  • 如果负数的计数是偶数,则返回 1,因为偶数个负数的乘积是正数。
  • 否则返回-1
public static int solution(int[] ints) {
    int count = 0;
    for (int i : ints) {
        if (i == 0) {
            return 0;
        }
        if (i < 0) {
            count++;
        }
    }
    return count % 2 == 0 ? 1 : -1;
}

数组乘法留给 OP。

查看Integer.signum() - 它将结果转换为 1、0、-1 以表示 >0、0、<0。

使用包装器实现,以及@Mr R 的建议

public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         int arr[] = {6,-7,8,9};
         int arr1[] = {-6,-7,8,9};
         int arr2[] = {0,-7,8,9};

         int[][] array_of_arrays = {arr, arr1, arr2};
         int[] results_array;
         results_array = solution_wrapper(array_of_arrays); 
         
         // then do what you neeed to do with the results_array
         
         System.out.println(results_array[0]);
         System.out.println(results_array[1]);
         System.out.println(results_array[2]);
         System.out.println("[ " + solution(arr) + " ]  => Needs to be -1" );
         System.out.println("[ " + solution(arr1) + " ] => Needs to be  1");
         System.out.println("[ " + solution(arr2) + " ] => Needs to be 0");
         System.out.println("Final return should be (-1, 1, 0)");
    }
    
    public static int solution(int[] A) {
        int temp = 1;
        for (int i = 0; i < A.length; i++){
            temp *= A[i];
        }
        return Integer.signum(temp);
    }

    public static int[] solution_wrapper(int[][] allA) {
        int[] rv = new int[allA.length];
        for (int j = 0; j < allA.length; j++) {
            rv[j] = solution(allA[j]);
        }
        return rv;
    }
}

您可以使用您创建的方法声明和初始化最终数组:

int[] finalArr = {solution(arr), solution(arr1), solution(arr2)};

但是,我不确定您要从哪里返回最终数组。 您有没有返回类型的主要方法和您的解决方案方法。 你确定你应该返回最终的数组,还是仅仅有一个就足够了?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM