计算一组数字的可能排列 [英] Calculate possible permutations of an array of numbers

查看:153
本文介绍了计算一组数字的可能排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSArray的数字{0,1,2,3}

计算4的阶乘(数组数),我有24可能的排列组合0,1,2,3

我想知道是否有办法计算所有这些可能的排列,并将它们放在一个单独的数组中。例如,给定上面的数字{0,1,2,3},结果排列如下:

  0123,0132,0213,0231,0312,0321,
1023,1032,1203,1230,1302,1320,
2013,2031,2103 ,2130,2301,2310,
3012,3021,3102,3120,3201,3210

任何帮助,非常感谢。非常感谢!

解决方案

我正在寻找代码,但我设法弄清楚:)如果有人需要它,代码如下:

  static NSMutableArray * results; 

void doPermute(NSMutableArray * input,NSMutableArray * output,NSMutableArray * used,int size,int level){
if(size == level){
NSString * word = [输出componentsJoinedByString:@];
[results addObject:word];
return;
}

++ ++;
$ b为(int i = 0; i< input.count; i ++){
if([used [i] boolValue]){
continue;
}

used [i] = [NSNumber numberWithBool:YES];
[output addObject:input [i]];
doPermute(输入,输出,已用,大小,等级);
used [i] = [NSNumber numberWithBool:NO];
[output removeLastObject];



NSArray * getPermutations(NSString * input,int size){
results = [[NSMutableArray alloc] init];

NSMutableArray * chars = [[NSMutableArray alloc] init];

$ b for(int i = 0; i< [input length]; i ++){
NSString * ichar = [NSString stringWithFormat:@%c,[input characterAtIndex:ⅰ]];
[chars addObject:ichar];


NSMutableArray * output = [[NSMutableArray alloc] init];
NSMutableArray * used = [[NSMutableArray alloc] init];

(int i = 0; i [使用的addObject:[NSNumber numberWithBool:NO]];


doPermute(chars,output,used,size,0);

返回结果;

$ / code>

使用


getPermutations(input,size)

获取一个存储了排列的NSArray。 b
$ b

例如:

  NSLog(@%@,getPermutations(@0123 ,4)); 

//控制台记录
结果:(
0123,
0132,
0213,
0231,
0312,
0321,
1023,
1032,
1203,
1230,
1302,
1320,
2013,
2031,
2103,
2130,
2301,
2310,
3012,
3021,
3102,
3120 ,
3201,
3210

我现在:)

I have an NSArray with the numbers {0,1,2,3}

Calculating the factorial of 4 (the count of the array), I have 24 possible permutations of 0,1,2,3

I would like to know if there is a way to calculate all of these possible permutations and place them in a separate array.

For example, given the numbers above, {0,1,2,3}, the resulting permutations would be:

0123, 0132, 0213, 0231, 0312, 0321,
1023, 1032, 1203, 1230, 1302, 1320,
2013, 2031, 2103, 2130, 2301, 2310,
3012, 3021, 3102, 3120, 3201, 3210

Any help is greatly appreciated. Thank you so much!

解决方案

I was looking for code, but I managed to figure it out :) If anyone else needs it, the code is as follows:

static NSMutableArray *results;

void doPermute(NSMutableArray *input, NSMutableArray *output, NSMutableArray *used, int size, int level) {
    if (size == level) {
        NSString *word = [output componentsJoinedByString:@""];
        [results addObject:word];
        return;
    }

    level++;

    for (int i = 0; i < input.count; i++) {
        if ([used[i] boolValue]) {
            continue;
        }

        used[i] = [NSNumber numberWithBool:YES];
        [output addObject:input[i]];
        doPermute(input, output, used, size, level);
        used[i] = [NSNumber numberWithBool:NO];
        [output removeLastObject];
    }
}

NSArray *getPermutations(NSString *input, int size) {
    results = [[NSMutableArray alloc] init];

    NSMutableArray *chars = [[NSMutableArray alloc] init];


    for (int i = 0; i < [input length]; i++) {
        NSString *ichar  = [NSString stringWithFormat:@"%c", [input characterAtIndex:i]];
        [chars addObject:ichar];
    }

    NSMutableArray *output = [[NSMutableArray alloc] init];
    NSMutableArray *used = [[NSMutableArray alloc] init];

    for (int i = 0; i < chars.count; i++) {
        [used addObject:[NSNumber numberWithBool:NO]];
    }

    doPermute(chars, output, used, size, 0);

    return results;
}

use

getPermutations(input, size)

to get an NSArray with the permutations stored.

For Example:

NSLog(@"%@", getPermutations(@"0123", 4));

//console log
RESULTS: (
    0123,
    0132,
    0213,
    0231,
    0312,
    0321,
    1023,
    1032,
    1203,
    1230,
    1302,
    1320,
    2013,
    2031,
    2103,
    2130,
    2301,
    2310,
    3012,
    3021,
    3102,
    3120,
    3201,
    3210
)

It's working perfect for me now :)

这篇关于计算一组数字的可能排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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