[英]iterating over a 2D array error
I'm having difficulty with a HackerRank Challenge. 我在使用HackerRank挑战时遇到了困难。 My code runs for most cases, but fails others.
我的代码运行大多数情况,但其他人失败。
The challenge is to find the Max Sum in a 2D array in the shape of an hour-glass that spans a 6 x 6 array. 挑战在于找到一个2D阵列中的Max Sum,其形状为一个跨越6 x 6阵列的沙漏形状。 With constraints being integer values of -9 to +9.
约束是整数值-9到+9。
Example: 例:
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
When I run my code with negative integers, I have a return statement of 0. 当我用负整数运行我的代码时,我的返回语句为0。
Here is my code: 这是我的代码:
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);
}
}
Any suggestions, hints, and/or solutions are welcomed! 欢迎任何建议,提示和/或解决方案!
You said you were interested in alternative solutions. 你说你对替代解决方案很感兴趣。 Here is one that makes heavy use of Java 8 streams, for your interest.
为了您的兴趣,这里大量使用Java 8流。 It's a lot longer (and less efficient) than your solution but arguably encapsulates the logic rather than having it embedded in the array calculations.
它比你的解决方案更长(效率更低),但可以说是封装逻辑而不是将其嵌入到数组计算中。
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;
}
}
And here is some code showing how it can be used: 以下是一些显示如何使用它的代码:
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);
The problem seems to be occurring because if negative values are being added, then it'll never be greater than the original value of maxSumValue
, which starts off as zero (Java will initialize it to zero by default since it was never initialized to anything). 问题似乎正在发生,因为如果添加负值,那么它将永远不会大于
maxSumValue
的原始值,它从零开始(默认情况下Java将其初始化为零,因为它从未初始化为任何东西) 。 A simple fix here is to just take the absolute value of maxSumHolder
when comparing it to maxSumValue
so that negative values will be taken into account. 这里一个简单的解决方法是在将
maxSumValue
与maxSumHolder
进行比较时获取maxSumValue
以便将负值考虑在内。 This, 这个,
if (maxSumHolder > maxSumValue || maxSumHolder == maxSumValue)
should be changed to 应改为
if (Math.abs(maxSumHolder) > maxSumValue || Math.abs(maxSumHolder) == maxSumValue)
However, if you're objective is not to find the sum with the greatest magnitude and you do feel that a smaller positive sum holds greater weight that a huge negative sum, then my suggestion would be to initialize maxSumValue
to the smallest possible number Java can hold. 但是,如果你的目标是找不到具有最大幅度的总和,并且你确实认为较小的正和值具有较大的权重,即一个巨大的负数,那么我的建议是将
maxSumValue
初始化为Java可以尽可能小的数量保持。 Change 更改
public static int maxSumValue;
to 至
public static int maxSumValue = -Integer.MAX_VALUE;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.