简体   繁体   English

在二维数组的一行中查找所需的总和java

[英]Finding the desired sum in a row of a 2d array java

I'm having trouble wrapping my head around how to implement a method that starts with an int[][] input and a predefined sum value that returns an int[][] output that only displays the adjacent values in the row that add up to the sum.我无法围绕如何实现一个方法开始到总和。 For example, if the input array was例如,如果输入数组是

int[][] input = new int[][]{
{3,4,4,2,7},
{2,3,2,8,6},
{1,4,2,1,2}
}

when the sum is set to 7, it would yield an output array with the following values当总和设置为 7 时,它将产生一个具有以下值的输出数组

{3,4,0,0,7},
{2,3,2,0,0},
{1,4,2,1,0}
}

The output array should only display values that add up to a set number (7) with their neighbors, or if its a number from 0-9 values that are the desired sum (when you search for 7 it displays all 7s).输出数组应该只显示与它们的邻居相加等于一组数字 (7) 的值,或者如果它是 0-9 值之间的数字是所需的总和(当您搜索 7 时,它会显示所有 7)。 To further clarify, as you can see in the output array, it displays the first two values, 3 and 4, because they add to 7. It displays 2,3,2 because they also add to 7. It prints out 1,4,2,1 on the bottom because 1,4,2 add to 7 and 4,2,1 add to 7. To summarize, it displays only values which are 7 or add up to 7 with the numbers next to them, and otherwise displays 0. Also the input values must be <0.为了进一步澄清,正如您在输出数组中看到的那样,它显示前两个值 3 和 4,因为它们与 7 相加。它显示 2,3,2,因为它们与 7 相加。它打印出 1,4 ,2,1 在底部,因为 1,4,2 加到 7 和 4,2,1 加到 7。总而言之,它只显示 7 或加起来为 7 的值,旁边是数字,否则显示 0。而且输入值必须 <0。

After brainstorming for a while I know that the code will need a number of nested loops: one to loop through the rows, inside that one to loop through the columns, inside that a loop to update the sum which stops when the sum equals the preset sum (7 in this case), and finally a loop to update the output[][].经过一段时间的头脑风暴,我知道代码将需要许多嵌套循环:一个循环遍历行,在那个循环中循环遍历列,在那个循环中更新总和,当总和等于预设时停止sum(在这种情况下为 7),最后是一个循环来更新输出 [][]。 However, I'm having a great deal of trouble on writing code that implements all of these loops correctly.但是,我在编写正确实现所有这些循环的代码时遇到了很多麻烦。 I am planning on doing the same thing with numbers that add up vertically, but want to focus on horizontal as of now.我打算对垂直相加的数字做同样的事情,但现在想专注于水平。

You need three nested loops.您需要三个嵌套循环。 Or really, three levels of nested loops with two loops after each other on the innermost level, but I will return to that in the end.或者真的,三层嵌套循环,最内层有两个循环,但我最后会回到那个。

Before any loops, create the backbone of your result array.在任何循环之前,创建结果数组的主干。 It's OK if it's an array of null references to arrays at this point.如果此时它是对数组的空引用的数组,那也没关系。

Outer loop: loop through the outer array.外循环:循环遍历外数组。 A classical for loop is one option.经典的for循环是一种选择。 For each entry, create an inner array of the correct length in the result, filled with zeroes for a start.对于每个条目,在结果中创建一个长度正确的内部数组,开始时填充零。

Middle loop, loop through the entries in the inner array.中间循环,循环遍历内部数组中的条目。 Again a classical for loop is fine.同样,经典的for循环很好。 Your goal is that each iteration will determine if a series of adjacent values with the sum 7 begins at this entry, and if so, find out how long it is, and copy the entries in question to the result array.您的目标是每次迭代将确定一系列总和为 7 的相邻值是否从该条目开始,如果是,找出它的长度,并将有问题的条目复制到结果数组中。

Inner loop (first inner loop, that is) is for adding adjacent entries to find out if the sum is 7. You start from the entry from the middle loop and add up the values.内循环(即第一个内循环)用于添加相邻条目以找出总和是否为 7。您从中间循环的条目开始并将值相加。 This loop has a double stop condition: you stop when the sum is 7 or greater (assuming all values are positive), but obviously you also stop when reaching the end of the inner array.这个循环有一个双重停止条件:当总和为 7 或更大时停止(假设所有值都是正的),但很明显,当到达内部数组的末尾时也会停止。 A while loop would be for my taste. while循环符合我的口味。 Only if the sum hits exactly 7, do you use a new inner loop to copy the values to the result.仅当总和恰好达到 7 时,才使用新的内部循环将值复制到结果中。

PS If you assume all values are positive, you shouldn't just assume, you should check. PS 如果你假设所有的值都是正的,你不应该只是假设,你应该检查。 In the middle loop, if the value in that entry is 0 or negative, throw an IllegalArgumentException .在中间循环中,如果该条目中的值为 0 或负数,则抛出IllegalArgumentException

This can be done in O(NxM) complexity.这可以以 O(NxM) 的复杂度完成。

For each row, you keep two indexes (say, iLow and iHigh initialised to the first item input [i][0]) and a sumSoFar variable.对于每一行,您保留两个索引(例如,iLow 和 iHigh 初始化为第一个项目输入 [i][0])和一个 sumSoFar 变量。 At each iteration, if the sumSoFar < 7 you increment iHigh.在每次迭代中,如果 sumSoFar < 7,则增加 iHigh。 If it is > 7 then you increment iLow and iHigh.如果它 > 7,则增加 iLow 和 iHigh。 If It is == 7 then you've found one combination + increment iLow and iHigh.如果它是 == 7,那么您已经找到了一种组合 + 增量 iLow 和 iHigh。

This should give the high-level idea of the algorithm and I'm sure that a lot of details are missing/incomplete but you should be able to put that in code.这应该给出算法的高级概念,我确信很多细节都缺失/不完整,但您应该能够将其放入代码中。

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

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