嗨,我想像这种格式在 nslog 上显示字符串组合
abc
bca
acb
and so on
但我的程序向我展示了这种格式排列 =
(
(
c,
b,
a
),
(
b,
c,
a
),
(
c,
a,
b
),
(
a,
c,
b
),
(
b,
a,
c
),
(
a,
b,
c
)
)
这是我正在使用的代码
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
NSMutableArray *permutations = nil;
int i = 0;
for (i = 0; i < array.count ; i++){
if (!permutations){
permutations = [NSMutableArray array];
for (NSString *character in array){
[permutations addObject:[NSArray arrayWithObject:character]];
}
} else {
//make copy of permutations array and clean og array
NSMutableArray *aCopy = [permutations copy] ;
[permutations removeAllObjects];
for (NSString *character in array){
//loop through the copy
for (NSArray *oldArray in aCopy){
//check if old string contains looping char..
if ([oldArray containsObject:character] == NO){
//update array
NSMutableArray *newArray = [NSMutableArray arrayWithArray:oldArray];
[newArray addObject:character];
//add to permutations
[permutations addObject:newArray];
}
}
}
}
}
NSLog(@"permutations = \n %@",permutations);
}
请告诉我如何以我的要求格式显示它