Ios 按字打印字符串的所有排列
我试图逐字打印字符串“This Is Demo”的所有排列。例如,有效的排列将是“此演示是”、“此演示是”、“此演示是”。 我的程序没有打印所有排列。代码怎么了Ios 按字打印字符串的所有排列,ios,objective-c,permutation,Ios,Objective C,Permutation,我试图逐字打印字符串“This Is Demo”的所有排列。例如,有效的排列将是“此演示是”、“此演示是”、“此演示是”。 我的程序没有打印所有排列。代码怎么了 +(void)printPermutations { NSString *str = @"This Is Demo"; NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSMutableA
+(void)printPermutations
{
NSString *str = @"This Is Demo";
NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *permutationInput = [arr mutableCopy];
[[self class] printPermutationOfString:permutationInput startIndex:0 endIndex:arr.count-1];
}
+(void)printPermutationOfString:(NSMutableArray*)arr startIndex:(int)startingindex
endIndex:(int)endIndex
{
if (startingindex == endIndex) {
NSLog(@"%@",arr);
return;
}
for (int i = startingindex; i < endIndex; i++) {
[arr exchangeObjectAtIndex:startingindex withObjectAtIndex:i];
[[self class] printPermutationOfString:arr startIndex:i+1 endIndex:endIndex];
[arr exchangeObjectAtIndex:startingindex withObjectAtIndex:endIndex];
}
}
+(void)打印置换
{
NSString*str=@“这是演示”;
NSArray*arr=[str组件由字符分隔集:[NSCharacterSet whitespaceCharacterSet]];
NSMUTABLEARRY*排列输入=[arr mutableCopy];
[[self class]PrintPermutationOfsString:permutationInput startIndex:0 endIndex:arr.count-1];
}
+(void)字符串的打印置换:(NSMutableArray*)arr startIndex:(int)startingindex
endIndex:(int)endIndex
{
如果(起始索引==结束索引){
NSLog(@“%@”,arr);
返回;
}
for(int i=startingindex;i
试试这个。为我工作。希望这有帮助
+ (void)printPermutations
{
NSString *str = @"This Is Demo";
NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *permutationInput = [arr mutableCopy];
[[self class] printPermutationOfString:permutationInput startIndex:0];
}
+(void)printPermutationOfString:(NSMutableArray*)arr startIndex:(int)startingindex
{
if (startingindex == arr.count) {
NSLog(@"%@",arr);
return;
}
for (int i = startingindex; i < arr.count; i++) {
[arr exchangeObjectAtIndex:startingindex withObjectAtIndex:i];
[[self class] printPermutationOfString:arr startIndex:startingindex+1];
if (i < (arr.count - 1))
[[self class] reverse:arr start:startingindex+1 end:(startingindex+1)+(arr.count-startingindex-2)]; // edited here
}
}
+ (void)reverse:(NSMutableArray *)arr start:(int)i end:(int)j {
if ([arr count] == 0)
return;
while (i < j) {
[arr exchangeObjectAtIndex:i
withObjectAtIndex:j];
i++;
j--;
}
}
+(void)打印置换
{
NSString*str=@“这是演示”;
NSArray*arr=[str组件由字符分隔集:[NSCharacterSet whitespaceCharacterSet]];
NSMUTABLEARRY*排列输入=[arr mutableCopy];
[[self class]PrintPermutationOfsString:permutationInput startIndex:0];
}
+(void)字符串的打印置换:(NSMutableArray*)arr startIndex:(int)startingindex
{
如果(起始索引==arr.count){
NSLog(@“%@”,arr);
返回;
}
对于(int i=起始索引;i
您的程序当前打印的是什么?你做了预期排列的图表吗?[0,1,2]、[0,2,1]等,并将其与您使用的索引进行比较?这将是可视化如何迭代的最好方法…它应该打印6个输出,分别是“这是演示”、“这是演示”、“这是演示”、“这是演示”、“这是演示”、“这是演示”。我只得到“这是演示”、“这是演示”检查我的答案是否有效,因为将endIndex设置为2导致for循环只执行两次,所以只得到2。我已修复了一个小问题。请使用编辑过的答案@aguptaPratyusha,是的,这很好,但是为什么你要做数组的反转。我遵循了这个网站给出的算法,如果它能帮助你,为什么你不接受这个答案