Java 如何迭代每个独立选择的排列?
假设我有一个列表Java 如何迭代每个独立选择的排列?,java,algorithm,recursion,Java,Algorithm,Recursion,假设我有一个列表[x1,x2,x3],其中x1,x2,x3可以取1到5之间的任何值 我想迭代所有可能创建的列表(从[1,1,1],[1,1,2],到[5,5,5])。这是一个简单的问题,列表中只有3个元素 您可以这样做: for x = 1; x <= 5; x++; for y = 1; y <= 5; y++; ... for q = 1; q <= 5; q++; create list [x, y,
[x1,x2,x3]
,其中x1
,x2
,x3
可以取1到5之间的任何值
我想迭代所有可能创建的列表(从[1,1,1]
,[1,1,2]
,到[5,5,5]
)。这是一个简单的问题,列表中只有3个元素
您可以这样做:
for x = 1; x <= 5; x++;
for y = 1; y <= 5; y++;
...
for q = 1; q <= 5; q++;
create list [x, y, ..., q];
do something with the list;
表示x=1;x至少在python中(如果是约束,则应指定语言):
至少在python中(如果是约束,则应指定语言):
在递归解决方案中,您不必每次都对列表进行排序。为递归函数提供排序列表必须足够。
为此,我编写了这段C代码。输出结果的长度将由len
确定。请记住,输入长度必须等于或大于len:
// Input must be sorted, Result must be initialized to empty list
void Iterate(List<int> input, int len, List<int> result)
{
if(result.Count == n)
print result
else
foreach (var i in input)
Iterate(input, len, result.Append(num).ToList())
}
//输入必须排序,结果必须初始化为空列表
无效迭代(列表输入、整数长度、列表结果)
{
如果(result.Count==n)
打印结果
其他的
foreach(输入中的var i)
迭代(输入、len、result.Append(num.ToList())
}
在递归解决方案中,您不必每次都对列表进行排序。为递归函数提供排序列表必须足够。
为此,我编写了这段C代码。输出结果的长度将由len
确定。请记住,输入长度必须等于或大于len:
// Input must be sorted, Result must be initialized to empty list
void Iterate(List<int> input, int len, List<int> result)
{
if(result.Count == n)
print result
else
foreach (var i in input)
Iterate(input, len, result.Append(num).ToList())
}
//输入必须排序,结果必须初始化为空列表
无效迭代(列表输入、整数长度、列表结果)
{
如果(result.Count==n)
打印结果
其他的
foreach(输入中的var i)
迭代(输入、len、result.Append(num.ToList())
}
使用您可以轻松完成:
public static void main(String[] args) {
int lowerBound = 1;
int upperBound = 5;
int setSize=3;
ContiguousSet<Integer> integers = ContiguousSet.create(Range.closed(lowerBound, upperBound), DiscreteDomain.integers());
List<Set<Integer>> sets = Lists.newArrayList();
for (int i = 0; i < setSize; i++) {
sets.add(integers);
}
Set<List<Integer>> cartesianProduct = Sets.cartesianProduct(sets);
for (List<Integer> list : cartesianProduct) {
System.out.println(list);
}
}
使用以下工具,您可以轻松完成:
public static void main(String[] args) {
int lowerBound = 1;
int upperBound = 5;
int setSize=3;
ContiguousSet<Integer> integers = ContiguousSet.create(Range.closed(lowerBound, upperBound), DiscreteDomain.integers());
List<Set<Integer>> sets = Lists.newArrayList();
for (int i = 0; i < setSize; i++) {
sets.add(integers);
}
Set<List<Integer>> cartesianProduct = Sets.cartesianProduct(sets);
for (List<Integer> list : cartesianProduct) {
System.out.println(list);
}
}
使用这个算法
输入:X为最小数,Y为最大数,Z为独立选择数
- 创建一个大小为Z的数组,每个元素等于X。称之为置换
- 循环:
- 将排列的副本添加到排列列表中
- 将J设置为Z减1
- 循环:
- 将1添加到置换[J]。如果置换[J]现在是Y或更小,则断开
- 将置换[J]设置为X
- 从J中减去1。如果J现在小于0,则返回置换列表
使用此算法
输入:X为最小数,Y为最大数,Z为独立选择数
- 创建一个大小为Z的数组,每个元素等于X。称之为置换
- 循环:
- 将排列的副本添加到排列列表中
- 将J设置为Z减1
- 循环:
- 将1添加到置换[J]。如果置换[J]现在是Y或更小,则断开
- 将置换[J]设置为X
- 从J中减去1。如果J现在小于0,则返回置换列表
必须是经典算法。但从头开始写总是很有趣的。下面是接受数据集和结果列表大小参数的Java类。核心方法是generate()。此外,还可以根据需要复制列表(更具功能性)
import com.google.common.collect.Maps;
导入org.apache.commons.lang.ArrayUtils;
导入java.util.Map;
公共类置换生成器{
私有int-listValuesSize;
私有int resultListSize;
私有字符串[]当前列表;
私有映射nextValue=Maps.newHashMap();
私有整数置换=0;
公共置换生成器(字符串[]数据集,int resultListSize){
this.listValuesSize=dataSet.length;
this.resultListSize=resultListSize;
init(数据集);
}
私有void init(字符串[]数据集){
//滚动价值
字符串previous=数据集[0];
对于(int-valuesIndex=1;valuesIndex到){
返回;
}
对于(int i=0;i
必须是经典算法。但从头开始写总是很有趣的。下面是接受数据集和结果列表大小参数的Java类。核心方法是generate()。此外,还可以根据需要复制列表(更具功能性)
import com.google.common.collect.Maps;
导入org.apache.commons.lang.ArrayUtils;
导入java.util.Map;
公共类置换生成器{
私有int-listValuesSize;
私有int resultListSize;
私有字符串[]当前列表;
私有映射nextValue=Maps.newHashMap();
私有整数置换=0;
公共置换生成器(字符串[]数据集,int resultListSize){
this.listValuesSize=dataSet.length;
this.resultListSize=resultListSize;
init(数据集);
}
公共关系
import com.google.common.collect.Maps;
import org.apache.commons.lang.ArrayUtils;
import java.util.Map;
public class PermutationGenerator {
private int listValuesSize;
private int resultListSize;
private String[] currentList;
private Map<String, String> nextValue = Maps.newHashMap();
private int permutations = 0;
public PermutationGenerator(String[] dataSet, int resultListSize) {
this.listValuesSize = dataSet.length;
this.resultListSize = resultListSize;
init(dataSet);
}
private void init(String[] dataSet) {
// rolling values
String previous = dataSet[0];
for (int valuesIndex = 1; valuesIndex < dataSet.length; valuesIndex++) {
nextValue.put(previous, dataSet[valuesIndex]);
previous = dataSet[valuesIndex];
}
nextValue.put(dataSet[dataSet.length - 1], dataSet[0]);
// init
currentList = new String[resultListSize];
for (int i = 0; i < resultListSize; i++) {
currentList[i] = dataSet[0];
}
}
public void generate() {
generate(0, resultListSize - 1);
}
private void generate(int from, int to) {
if (from > to) {
return;
}
for (int i = 0; i < listValuesSize; i++) {
if (from == to) {
processList(currentList);
} else {
generate(from + 1, to);
}
roll(from);
}
}
private void roll(int position) {
currentList[position] = nextValue.get(currentList[position]);
}
private void processList(String[] list) {
permutations++;
System.out.println(ArrayUtils.toString(list));
}
public static void main(String... args) {
PermutationGenerator generator = new PermutationGenerator(new String[]{"1", "2", "3", "4", "5"}, 3);
generator.generate();
System.out.println(generator.permutations);
}
}
public class permutation {
static int limit;
public static void permutate(int arr[],int curPos)
{
int i;
if(curPos==arr.length)
{
for(i=0;i<arr.length;i++)
{
System.out.print(arr[i] + "\t");
}
System.out.println("");
return;
}
for(i=1;i<=limit;i++)
{
arr[curPos]=i;
permutate(arr,curPos+1);
}
}
public static void main(String[] args) {
int arr[] = new int[3];
limit = 5;
permutate(arr,0);
}
}
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 2 1
1 2 2
1 2 3
1 2 4
1 2 5
1 3 1
1 3 2
1 3 3
1 3 4
1 3 5
1 4 1
1 4 2
1 4 3
1 4 4
1 4 5
1 5 1
1 5 2
1 5 3
1 5 4
1 5 5
2 1 1
2 1 2
2 1 3
2 1 4
2 1 5
2 2 1
2 2 2
2 2 3
2 2 4
2 2 5
2 3 1
2 3 2
2 3 3
2 3 4
2 3 5
2 4 1
2 4 2
2 4 3
2 4 4
2 4 5
2 5 1
2 5 2
2 5 3
2 5 4
2 5 5
3 1 1
3 1 2
3 1 3
3 1 4
3 1 5
3 2 1
3 2 2
3 2 3
3 2 4
3 2 5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5
3 4 1
3 4 2
3 4 3
3 4 4
3 4 5
3 5 1
3 5 2
3 5 3
3 5 4
3 5 5
4 1 1
4 1 2
4 1 3
4 1 4
4 1 5
4 2 1
4 2 2
4 2 3
4 2 4
4 2 5
4 3 1
4 3 2
4 3 3
4 3 4
4 3 5
4 4 1
4 4 2
4 4 3
4 4 4
4 4 5
4 5 1
4 5 2
4 5 3
4 5 4
4 5 5
5 1 1
5 1 2
5 1 3
5 1 4
5 1 5
5 2 1
5 2 2
5 2 3
5 2 4
5 2 5
5 3 1
5 3 2
5 3 3
5 3 4
5 3 5
5 4 1
5 4 2
5 4 3
5 4 4
5 4 5
5 5 1
5 5 2
5 5 3
5 5 4
5 5 5