簡體   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