Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 获取二进制数组中最大和最小的一组_Java_Arrays - Fatal编程技术网

Java 获取二进制数组中最大和最小的一组

Java 获取二进制数组中最大和最小的一组,java,arrays,Java,Arrays,我有一个0和1的二维数组 具有1的每个连续单元被视为单个对象 我的任务是找到每行最大的连续1,以及最小的连续1,并得到它们之间的差异 Example: oth Row --> 0 0 0 0 0 1st Row --> 0 0 1 0 0 2nd Row --> 0 0 1 0 1 3rd Row --> 1 1 1 0 1 4th Row --> 0 0 0 0 0 说明: Sum of largest objects = 0 (0th row) + 1

我有一个0和1的二维数组

具有1的每个连续单元被视为单个对象

我的任务是找到每行最大的连续1,以及最小的连续1,并得到它们之间的差异

Example:

oth Row --> 0 0 0 0 0
1st Row --> 0 0 1 0 0 
2nd Row --> 0 0 1 0 1
3rd Row --> 1 1 1 0 1 
4th Row --> 0 0 0 0 0 
说明:

Sum of largest objects = 0 (0th row) + 1 (1st row) + 1 (2nd row) + 3 (3rd row) + 0 (4th row) = 5
Sum of smallest objects = 0 (0th row) + 0 (1st row) + 1 (2nd row) + 1 (3rd row) + 0 (4th row) = 2

Result = sum of largest - sum of smallest = 5 - 2 = 3.
第0行中没有1,因此不需要操作

在第一行有一个1,所以我认为这一行最大的对象是1

在第二排的位置[2,2]有一个1,我认为这一排最大的物体是1。现在位置[2,4]的下一个1,我认为它是这一行中最小的对象,是1

在第三排,在[3,0]、[3,1]、[3,2]位置有三个1,我认为这一排最大的物体是3。现在位置[3,4]的下一个1,我认为它是这一行中最小的对象,是1

第四排没有1,因此不需要操作

我的任务是找出所有最大对象和所有最小对象之和之间的差异

在本例中:

Sum of largest objects = 0 (0th row) + 1 (1st row) + 1 (2nd row) + 3 (3rd row) + 0 (4th row) = 5
Sum of smallest objects = 0 (0th row) + 0 (1st row) + 1 (2nd row) + 1 (3rd row) + 0 (4th row) = 2

Result = sum of largest - sum of smallest = 5 - 2 = 3.
我提出了以下逻辑:

public static int process(int[][] array)
{
    int result = 0;
    // Get row and column size
    int rows = array.length;
    int cols = array[0].length;

    // variables to store sum of large and small objects
    int large = 0;
    int small = 0;

    for(int i=0; i<rows; i++) {
        // variables to store large and small objects per row level
        int l_count = 0;
        int s_count = 0;


        boolean valid = false;
        for(int j=0; j<cols-1; j++) {
            int curr = array[i][j];
            int next = array[i][j+1];
            // First occurrence of 1 is considered as large object
            if(l_count == 0 && curr == 1) {
                l_count++;
                valid = true;
            }
            // consecutive 1's added to large count
            if(valid && curr == next) {
                l_count++;
            } else {
                valid = false;
            }

            // Get small count
            if(!valid && curr == 1 || next == 1) {
                s_count++;
            }
        }
        // Add row level counts to large and small objects
        large += l_count;
        small += s_count;

    }
    result = large - small;
  return result;
}
公共静态int进程(int[][]数组)
{
int结果=0;
//获取行和列的大小
int rows=array.length;
int cols=数组[0]。长度;
//用于存储大小对象总和的变量
int-large=0;
int小=0;

对于(int i=0;i也许下面的更改可以解决您的问题。不要查看下一个元素,只需保持计数并始终使用当前元素即可

public static int process(int[][] array)
{
    int result = 0;
    // Get row and column size
    int rows = array.length;
    int cols = array[0].length;

    // variables to store sum of large and small objects
    int large = 0;
    int small = 0;

    for(int i=0; i<rows; i++) {
        // variables to store large and small objects per row level
        int l_count = 0;
        int s_count = 0;
        int count = 0;

        for(int j=0; j<cols; j++) {
            if (array[i][j]) {
                count++;
            } else if (count > 0) {
                if (count > l_count) {
                    l_count = count;
                } else if (count < s_count || s_count == 0) {
                    s_count = count;
                }
                count = 0;
            }
        }
        // Add row level counts to large and small objects
        large += l_count;
        small += s_count;

    }
    result = large - small;
  return result;
}
公共静态int进程(int[][]数组)
{
int结果=0;
//获取行和列的大小
int rows=array.length;
int cols=数组[0]。长度;
//用于存储大小对象总和的变量
int-large=0;
int小=0;
for(int i=0;i l_计数){
l_计数=计数;
}else if(count
要查找每行中最大和最小的对象,您可能首先需要获取该行中所有对象的列表,然后进行相应的筛选,也就是说,有一个函数可以

0 0 0 0 0 -> []
0 0 1 0 0 -> [1]
0 0 1 0 1 -> [1,1]
1 1 1 0 1 -> [3,1]
0 0 0 0 0 -> []
之后,您可以使用诸如
Collections.max(collection)
Collections.min(collection)
之类的函数来获取最大值和最小值。要考虑第1行中的情况,您可以检查
if(foundObjects.size<2)
并在这种情况下将
min
设置为
0

假设您使用的是Java 1.8或更高版本,可能的实现如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class ObjectCalculator {
    static int compute(int[][] array){
        return Arrays.stream(array)
                .map(ObjectCalculator::findObjects)
                .mapToInt(objects -> {
                    int min, max;
                    if (objects.size() < 2) min = 0;
                    else min = Collections.min(objects);
                    if (objects.size() < 1) max = 0;
                    else max = Collections.max(objects);
                    return max - min;
                }).sum();
    }

    static List<Integer> findObjects(int[] array) {
        ArrayList<Integer> objects = new ArrayList<>();
        int curSize = 0;
        for(int i : array){
            if(i == 1)curSize++;
            else if(curSize > 0){
                objects.add(curSize);
                curSize = 0;
            }
        }
        if(curSize > 0){
            objects.add(curSize);
        }
        return objects;
    }
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
类对象计算器{
静态int计算(int[][]数组){
返回数组。流(数组)
.map(ObjectCalculator::findObjects)
.mapToInt(对象->{
最小整数,最大整数;
如果(objects.size()<2)min=0;
else min=Collections.min(对象);
如果(objects.size()<1)max=0;
else max=Collections.max(对象);
返回最大-最小值;
}).sum();
}
静态列表FindObject(int[]数组){
ArrayList对象=新的ArrayList();
int curSize=0;
for(int i:array){
如果(i==1)将++;
else if(光标>0){
对象。添加(光标化);
草书=0;
}
}
如果(光标>0){
对象。添加(光标化);
}
归还物品;
}
}

第一行中最小的对象不是与最大的对象相同,因此大小为1吗?关于第一行-同一序列不能既是最长的序列又是最短的序列?当对一行进行迭代时,您已经发现一个1序列,并认为它是最长的,然后当第二个1序列更长时,则是以前最长的序列quence现在成为最短的序列…@AUsername,首先我们填充最大的,然后我们寻找最小的,所以在第一行中最小的是0@JaroslawPawlak,我在上面添加了一条注释,请检查我的解决方案是获取行中所有对象的列表,然后调用Collections.max()和Collections.min()但是,这并不像您描述的那样。如何首先获取
集合
对象,您能解释一下吗?
导入java.util.Collections