繁体   English   中英

迭代2D数组错误

[英]iterating over a 2D array error

我在使用HackerRank挑战时遇到了困难。 我的代码运行大多数情况,但其他人失败。

挑战在于找到一个2D阵列中的Max Sum,其形状为一个跨越6 x 6阵列的沙漏形状。 约束是整数值-9到+9。

例:

0 2 4 5 1 2 0 2 3 3 2 0 1 4 0 8 6 4 With 8 6 4 0 2 1 4 7 1 7 = 8 + 6 + 4 + 7 + 6 + 2 + 7 = 40 5 0 3 6 2 7 6 2 7 6 3 2 2 0 1

当我用负整数运行我的代码时,我的返回语句为0。

这是我的代码:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static int maxSumValue;
    public static int y;
    public static int maxSumHolder;


    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int arr[][] = new int[6][6];
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 6; j++) {
            arr[i][j] = in.nextInt();
        }
    }
    for (int x = 0; x < 4; x++) {
        for (int y = 0; y < 4; y++){
            maxSumHolder = arr[x][y] + arr[x][y + 1] + arr[x][y + 2]
                    + arr[x + 1][y + 1] + arr[x + 2][y] + arr[x + 2][y + 1] + arr[x + 2][y + 2];
            if (maxSumHolder > maxSumValue || maxSumHolder == maxSumValue){
                maxSumValue = maxSumHolder;
            }
        }
    }
    System.out.println(maxSumValue);
}
}

欢迎任何建议,提示和/或解决方案!

你说你对替代解决方案很感兴趣。 为了您的兴趣,这里大量使用Java 8流。 它比你的解决方案更长(效率更低),但可以说是封装逻辑而不是将其嵌入到数组计算中。

class Position {
    public static final int SIZE = 6;
    private final int row;
    private final int col;

    private Position(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public static Stream<Position> all() {
        return IntStream.range(0, SIZE).boxed()
                .flatMap(row -> IntStream.range(0, SIZE)
                        .mapToObj(col -> new Position(row, col)));
    }

    public static Stream<Position> allNonEdge() {
        return all().filter(Position::notOnEdge);
    }

    private boolean notOnEdge() {
        return row > 0 && col > 0 && row < SIZE - 1 || col < SIZE - 1;
    }

    public int shapeSum(int[][] array) {
        return all().filter(this::isInShape)
                .mapToInt(pos -> pos.getVal(array))
                .sum();
    }

    private boolean isInShape(Position other) {
        int rowdiff = Math.abs(this.row - other.row);
        int coldiff = Math.abs(this.col - other.col);
        return rowdiff == 0 && coldiff == 0 || rowdiff == 1 && coldiff <= 1;
    }

    public int getVal(int[][] array) {
        return array[row][col];
    }

    public void setVal(int[][] array, int val) {
        array[row][col] = val;
    }    
}

以下是一些显示如何使用它的代码:

    Random rand = new Random();
    int[][] array = new int[Position.SIZE][Position.SIZE];
    Position.all().forEach(pos -> pos.setVal(array, rand.nextInt(100)));
    Position.allNonEdge()
            .mapToInt(pos -> pos.shapeSum(array))
            .max()
            .ifPresent(System.out::println);

问题似乎正在发生,因为如果添加负值,那么它将永远不会大于maxSumValue的原始值,它从零开始(默认情况下Java将其初始化为零,因为它从未初始化为任何东西) 。 这里一个简单的解决方法是在将maxSumValuemaxSumHolder进行比较时获取maxSumValue以便将负值考虑在内。 这个,

if (maxSumHolder > maxSumValue || maxSumHolder == maxSumValue)

应改为

if (Math.abs(maxSumHolder) > maxSumValue || Math.abs(maxSumHolder) == maxSumValue)

但是,如果你的目标是找不到具有最大幅度的总和,并且你确实认为较小的正和值具有较大的权重,即一个巨大的负数,那么我的建议是将maxSumValue初始化为Java可以尽可能小的数量保持。 更改

public static int maxSumValue;

public static int maxSumValue = -Integer.MAX_VALUE;

暂无
暂无

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

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