Iphone 如何从Objective-C中的数组中获得所有可能的数字组合
可能重复: 假设我有Iphone 如何从Objective-C中的数组中获得所有可能的数字组合,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,可能重复: 假设我有 [1,2] 我想得到 {1} {2} {1, 2} {2, 3} 我想你要找的东西的名字是“电源”。这听起来很有趣,所以这里有一个裂缝。我依靠的是算法。我不确定这是否有效……事实上,我确定这在大型设备上是无效的 // answer the powerset of array: an array of all possible subarrays of the passed array - (NSArray *)powerSet:(NSArray *)array {
[1,2]
我想得到
{1}
{2}
{1, 2}
{2, 3}
我想你要找的东西的名字是“电源”。这听起来很有趣,所以这里有一个裂缝。我依靠的是算法。我不确定这是否有效……事实上,我确定这在大型设备上是无效的
// answer the powerset of array: an array of all possible subarrays of the passed array
- (NSArray *)powerSet:(NSArray *)array {
NSInteger length = array.count;
if (length == 0) return [NSArray arrayWithObject:[NSArray array]];
// get an object from the array and the array without that object
id lastObject = [array lastObject];
NSArray *arrayLessOne = [array subarrayWithRange:NSMakeRange(0,length-1)];
// compute the powerset of the array without that object
// recursion makes me happy
NSArray *powerSetLessOne = [self powerSet:arrayLessOne];
// powerset is the union of the powerSetLessOne and powerSetLessOne where
// each element is unioned with the removed element
NSMutableArray *powerset = [NSMutableArray arrayWithArray:powerSetLessOne];
// add the removed object to every element of the recursive power set
for (NSArray *lessOneElement in powerSetLessOne) {
[powerset addObject:[lessOneElement arrayByAddingObject:lastObject]];
}
return [NSArray arrayWithArray:powerset];
}
如果您认为这是一个keeper,那么可以将其作为数组上的category方法并删除该参数。像这样测试它
NSLog(@"empty = %@", [self powerSet:[NSArray array]]);
NSLog(@"one item = %@", [self powerSet:[NSArray arrayWithObject:@"a"]]);
NSLog(@"two items = %@", [self powerSet:[NSArray arrayWithObjects:@"a", @"b", nil]]);
NSLog(@"three items = %@", [self powerSet:[NSArray arrayWithObjects:@"a", @"b", @"c", nil]]);
我只做了这些测试,结果看起来不错。扰流板警报:在我的控制台上,三项测试大致如下所示,但已删除:
三项=,,
A.
B
a、 b,,
C
a、 c,
公元前
a、 b,c
我想你要找的东西的名字是“电源”。这听起来很有趣,所以这里有一个裂缝。我依靠的是算法。我不确定这是否有效……事实上,我确定这在大型设备上是无效的
// answer the powerset of array: an array of all possible subarrays of the passed array
- (NSArray *)powerSet:(NSArray *)array {
NSInteger length = array.count;
if (length == 0) return [NSArray arrayWithObject:[NSArray array]];
// get an object from the array and the array without that object
id lastObject = [array lastObject];
NSArray *arrayLessOne = [array subarrayWithRange:NSMakeRange(0,length-1)];
// compute the powerset of the array without that object
// recursion makes me happy
NSArray *powerSetLessOne = [self powerSet:arrayLessOne];
// powerset is the union of the powerSetLessOne and powerSetLessOne where
// each element is unioned with the removed element
NSMutableArray *powerset = [NSMutableArray arrayWithArray:powerSetLessOne];
// add the removed object to every element of the recursive power set
for (NSArray *lessOneElement in powerSetLessOne) {
[powerset addObject:[lessOneElement arrayByAddingObject:lastObject]];
}
return [NSArray arrayWithArray:powerset];
}
如果您认为这是一个keeper,那么可以将其作为数组上的category方法并删除该参数。像这样测试它
NSLog(@"empty = %@", [self powerSet:[NSArray array]]);
NSLog(@"one item = %@", [self powerSet:[NSArray arrayWithObject:@"a"]]);
NSLog(@"two items = %@", [self powerSet:[NSArray arrayWithObjects:@"a", @"b", nil]]);
NSLog(@"three items = %@", [self powerSet:[NSArray arrayWithObjects:@"a", @"b", @"c", nil]]);
我只做了这些测试,结果看起来不错。扰流板警报:在我的控制台上,三项测试大致如下所示,但已删除:
三项=,,
A.
B
a、 b,,
C
a、 c,
公元前
a、 b,c
{2,3}从何而来?在网上或数学书中查找排列和组合。@nspostwenidle等人。-问题似乎是关于幂集所有可能的子集,而不是排列所有可能的顺序,就像假定的副本一样。这些是不同的概念。{2,3}从何而来?在网上或数学书中查找排列和组合。@nspostwenidle等人。-问题似乎是关于幂集所有可能的子集,而不是排列所有可能的顺序,就像假定的重复中的情况一样。这些是不同的概念。这不是我要找的,它还需要显示b,a,c,a,c,b,c,b,a等。你要找的是排列而不是组合。这不是我要找的,它还需要显示b,a,c,a,c,b,b,a等。你要的是排列而不是组合。