简体   繁体   English

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

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

I'm practicing Java and my prof has given me this problem:我正在练习 Java,我的教授给了我这个问题:

You're given an array A of N integers (between -100 and 100), calculate the multiplied value of all elements inside the array and return (-1, 0, 1) based on the output.给定一个包含 N 个整数(介于 -100 和 100 之间)的数组 A,计算数组内所有元素的相乘值并根据 output 返回 (-1, 0, 1)。

For instance:例如:

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)

Method Definition:方法定义:

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

My approach: I was thinking this would be a recursive process because I will multiple have inputs.我的方法:我认为这将是一个递归过程,因为我将有多个输入。

Process one block A[1,2,3], store the value (1) into a newArr[], process another block A[-1,2,3], store the value (-1) into newArr[] and so on... at the end return newArr[] with the proper values in it.处理一个块 A[1,2,3],将值 (1) 存储到 newArr[],处理另一个块 A[-1,2,3],将值 (-1) 存储到 newArr[] 等等on... 最后返回 newArr[] ,其中包含正确的值。

What I have so far, now I am kind of stuck..到目前为止我所拥有的,现在我有点卡住了..

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;
    }

I don't know if this is the right way to proceed, any ideas as to how I should go about doing this?我不知道这是否是正确的方法,关于我应该如何 go 这样做的任何想法?

Thanks in advance!提前致谢!

No need to do any multiplication.无需进行任何乘法运算。

  • if any number is 0 return 0 immediately since the product will be zero.如果任何数字为0 ,则立即返回0 ,因为乘积为零。
  • if the count of negative numbers is even, return 1 since the product of an even number of negatives is a positive.如果负数的计数是偶数,则返回 1,因为偶数个负数的乘积是正数。
  • else return -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;
}

Array multiplication left to OP.数组乘法留给 OP。

Look at Integer.signum() - which will convert the result to 1, 0, -1 for >0, 0, <0.查看Integer.signum() - 它将结果转换为 1、0、-1 以表示 >0、0、<0。

Implementing with a wrapper, along with @Mr R's suggestion使用包装器实现,以及@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;
    }
}

You can declare and initialize a final array by using the method you created:您可以使用您创建的方法声明和初始化最终数组:

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

However, I'm not sure where you want to return the final array from.但是,我不确定您要从哪里返回最终数组。 You have your main method which doesn't have a return type and your solution method.您有没有返回类型的主要方法和您的解决方案方法。 Are you certain that you're supposed to return the final array, or is simply having one enough?你确定你应该返回最终的数组,还是仅仅有一个就足够了?

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

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