没有在java中重复/置换发生变化 [英] generating Variations without repetitions / Permutations in java

查看:119
本文介绍了没有在java中重复/置换发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要生成所有的变化,而不采用数字重复0 - 9

I have to generate all variations without repetitions made of digits 0 - 9.

他们的长度可以从1到10我真的不知道如何解决这个问题,尤其是如何避免重复。

Length of them could be from 1 to 10. I really don't know how to solve it, especially how to avoid repetitions.

例:    变化的长度:4    随机变化:9856,8753,1243,1234等(但不包括9985 - 含重复)

Example: length of variations: 4 random variations: 9856, 8753, 1243, 1234 etc. (but not 9985 - contains repetition)

我会很感激,如果有人能帮助我这个问题,特别是给一些code和线索。

I would be really grateful if somebody can help me with that issue, especially giving some code and clues.

推荐答案

要查找的关键字的排列的。有源$ C ​​$ C丰富免费提供执行它们。

The keyword to look for is permutation. There is an abundance of source code freely available that performs them.

至于保持其重复分类,我建议一个简单的递归的方法:对于每一个数字,你有考虑到你的变化与否的选择,让你的递归通过数字和叉成两个递归调用,一是计算在其中数字被包括在内,它在其中被排除在外。然后,你到达最后一位数字后每次递归基本上是给你一个(独特的,排序)的重复无位名单。然后,您可以创建这个列表中的所有可能的排列和组合所有这些置换来实现自己的最终结果。

As for keeping it repetition free I suggest a simple recursive approach: for each digit you have a choice of taking it into your variation or not, so your recursion counts through the digits and forks into two recursive calls, one in which the digit is included, one in which it is excluded. Then, after you reached the last digit each recursion essentially gives you a (unique, sorted) list of repetition-free digits. You can then create all possible permutations of this list and combine all of those permutations to achieve your final result.

(同duffymo说:我将不提供code为)

(Same as duffymo said: I won't supply code for that)

高级注:递归基于0/1(排斥,夹杂物),它可以直接被转换为位,因此,整数。因此,为了得到所有可能的数字组合,没有实际执行递归本身,你可以简单地使用所有的10位整数,并遍历它们。然后除$ P $角的数,且有一组比特对应于包括需要被置换列表中的位

Advanced note: the recursion is based on 0/1 (exclusion, inclusion) which can directly be translated to bits, hence, integer numbers. Therefore, in order to get all possible digit combinations without actually performing the recursion itself you could simply use all 10-bit integer numbers and iterate through them. Then interpret the numbers such that a set bit corresponds to including the digit in the list that needs to be permuted.

这篇关于没有在java中重复/置换发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆