[英]Given an array of length N, where each element in the array has X possibilities, return all possible arrays
I'm trying to solve the above problem using Java.我正在尝试使用 Java 解决上述问题。 Thought about using for loops but I can't see how this would work without knowing X and N in advance.
考虑过使用 for 循环,但如果事先不知道 X 和 N,我无法看到这将如何工作。 I'm trying to solve it with recursion but not sure how it looks.
我试图用递归来解决它,但不确定它的外观。 Been trying for a couple hours now.
已经尝试了几个小时了。 Any help is greatly appreciated.
任何帮助是极大的赞赏。
Example, if N = 4 and x = 3, possible arrays are:例如,如果 N = 4 且 x = 3,则可能的数组为:
[0,0,0,1]
[2,1,0,0]
[1,2,1,1]
[0,2,2,2]
First of all, all of those who say "use debugger", this is not about the code... it is about the understanding.首先,所有说“使用调试器”的人,这不是关于代码……而是关于理解。 he asks for direction, i would understand if you down vote because he didn't show his code, that something else and you can write it...
他询问方向,我会理解如果你投反对票,因为他没有展示他的代码,还有别的东西,你可以写......
My algorithem works like counting, you start counting until X (basiclly like counting in Base X) and when you get to X you add 1 to the next position in the Array
and reset the last position.我的算法就像计数一样,你开始计数直到 X(基本上就像在 Base X 中计数),当你到达 X 时,你将 1 添加到
Array
的下一个位置并重置最后一个位置。
so, as far as i know you can't return few arrays in a normal way so i'll assume you want to print them.所以,据我所知,你不能以正常方式返回几个数组,所以我假设你想打印它们。
now, this is a "nice" code:现在,这是一个“不错”的代码:
http://pastebin.com/Uqv3fMxg http://pastebin.com/Uqv3fMxg
few notes:几个注意事项:
you should not make the function static, i did it from laziness.你不应该让函数成为静态的,我是因为懒惰才这么做的。
this is not perfect, it print twice [1,0], [2,0] and so on.这并不完美,它打印了两次 [1,0]、[2,0] 等等。 couln't find the problem and it is way too late for me, i'll try to check it again tommorow.
找不到问题,对我来说太晚了,我明天再试试看。
i'm not a guy that comments his code, i did a little but i'll explain everything i can here.我不是一个评论他的代码的人,我做了一点,但我会在这里解释我能做的一切。
The program starts in the main method where i defined the Array
and started the recurtion method.程序从主方法开始,我定义了
Array
并启动了递归方法。
The stop statement is when i got into the N position (that doesn't exist in the array, only N-1)停止语句是当我进入 N 位置时(数组中不存在,只有 N-1)
The parameters: the array, the position in the array that i am corrently counting and a "mode", is it a run to go to the next position or just to print all of the number in this position.参数:数组,我正在计算的数组中的位置和“模式”,是运行到下一个位置还是只是打印该位置的所有数字。
The alreadyHas
variable is ment to not print number 2 times. alreadyHas
变量不能打印数字 2 次。
if i won't use it, it would print every number atleast 2 times, it will redo any work he has done until he move to this position and every position.如果我不使用它,它会打印每个数字至少 2 次,它会重做他所做的任何工作,直到他移动到这个位置和每个位置。 it is hard to explain, you can write 0 instead of it and see what happens.
很难解释,你可以写 0 而不是它,看看会发生什么。
so, i count until X, every time i print the new Array
and to count every number and not skiping any i have to recount the last position with a different number in the last position.所以,我一直数到 X,每次我打印新
Array
并计算每个数字而不是跳过任何数字时,我必须在最后一个位置用不同的数字重新计算最后一个位置。
after i have count all of the possible numbers in this possition, i am giving it a value of 0 and going to the next position.在我计算了这个位置中所有可能的数字后,我给它一个值 0 并转到下一个位置。
so,to sum it up, you count the number in the first position of the array, when you reach the maximum you add a carry in the next position and reset the corrent position.所以,总而言之,您计算数组第一个位置的数字,当您达到最大值时,您在下一个位置添加进位并重置相应位置。
I'm sorry if something is not clear, English is not my native language so i have a difficulities in explaining myself.如果有什么不清楚,我很抱歉,英语不是我的母语,所以我很难解释自己。
you can ask me any question if something is not clear and i'll answer, it will be easier way to explain.如果有什么不清楚的地方,你可以问我任何问题,我会回答,这会更容易解释。
good night :)晚安 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.