Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
16
Task/Power-set/Objective-C/power-set.m
Normal file
16
Task/Power-set/Objective-C/power-set.m
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#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[itemIndex]];
|
||||
}
|
||||
}
|
||||
[subsets addObject:subset];
|
||||
}
|
||||
return subsets;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue