tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
17
Task/Power-set/Objective-C/power-set.m
Normal file
17
Task/Power-set/Objective-C/power-set.m
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
+ (NSArray *)powerSetForArray:(NSArray *)array {
|
||||
UInt32 subsetCount = 1 << array.count;
|
||||
NSMutableArray *subsets = [NSMutableArray arrayWithCapacity:subsetCount];
|
||||
for(int subsetIndex = 0; subsetIndex < subsetCount; subsetIndex++) {
|
||||
NSMutableArray *subset = [[NSMutableArray alloc] init];
|
||||
for (int itemIndex = 0; itemIndex < array.count; itemIndex++) {
|
||||
if((subsetIndex >> itemIndex) & 0x1) {
|
||||
[subset addObject:[array objectAtIndex:itemIndex]];
|
||||
}
|
||||
}
|
||||
[subsets addObject:subset];
|
||||
[subset release];
|
||||
}
|
||||
return subsets;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue