简体   繁体   English

给定一个长度为 N 的数组,其中数组中的每个元素都有 X 种可能性,返回所有可能的数组

[英]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.

相关问题 给定一个整数数组(长度为 n),使用 Java 中的递归查找并返回输入数组的所有子集 - Given an integer array (of length n), find and return all the subsets of input array using recursion in Java 给定n个字节的数据,计算不同的位阵列可能性 - Calculate distinct bit-array possibilities given n bytes of data 给定数字n作为输入,返回长度为n的新字符串数组,其中包含字符串“ 0”,“ 1”,“ 2”,依此类推,直到n-1 - Given a number n as input, return a new string array of length n, containing the strings “0”, “1”, “2” so on till n-1 给定大小为 n 的数组中 r 个元素的所有可能组合,并带有条件 - All possible combinations of r elements in a given array of size n with conditions 从长度为 n 的字符数组生成长度为 m 的所有子序列,其中 n >= m 在 Java 语言中 - Generate all subsequences of length m from a character array of length n where n >= m in Java language 如何在 Java 中返回给定数组的所有可能排列? - How can I return all possible permutations of a given array in Java? 如何将给定的数字列表划分为 N 个数组,其中每个数组的总和最接近特定数字 M? - How to partition a given list of numbers to N array where the sum of each array is closest to a specific number, M? 给定一个正整数数组。 将其分为2个数组,每个数组的平均值相同 - Given an array of positive integers. Divide it into 2 arrays where avg of each array is same 返回给定数组的一个版本,其中数组中的每个零值都被数组中零右侧的最大奇数值替换 - Return a version of the given array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array 检查整数数组是否对数组中的每个值 n 都有,还有一个元素 2n, 2n+1, n/2。 满足则返回1,不满足则返回0 - Check if an integer array has for every value n in the array, there is also an element 2n, 2n+1, n/2. Return 1 if it satisfies and 0 if it doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM