这是我的要求:
forSaleSingleProperties
数组应包含没有相同属性PARCELID
forSaleMultipleProperties
数组应包含具有相同属性PARCELID
forSalePropertiesArray
是包含所有字典的基本数组。
注意:词典包含各种其他属性。我想要所有具有相同PARCELID
属性的人
我不明白这个逻辑有什么问题......
if (_forSaleSinglePropertiesArray==nil) {
_forSaleSinglePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSaleMultiplePropertiesArray==nil) {
_forSaleMultiplePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSalePropertiesArray!=nil) {
if (_forSalePropertiesArray.count>1) {
BOOL propertyObject1IsMultiple = NO;
NSDictionary *propertyObject1;
NSMutableArray *multiplePinArray = [[NSMutableArray alloc]initWithObjects: nil];
for (int i=0; i<_forSalePropertiesArray.count; i++) {
propertyObject1 = [_forSalePropertiesArray objectAtIndex:i];
multiplePinArray = nil;
multiplePinArray = [[NSMutableArray alloc]initWithObjects: nil];
for (int j=i+1; j<_forSalePropertiesArray.count; j++) {
NSDictionary *propertyObject2 = [_forSalePropertiesArray objectAtIndex:j];
if ([propertyObject1 valueForKey:PARCEL_ID]==[propertyObject2 valueForKey:PARCEL_ID]) {
if (_forSaleMultiplePropertiesArray.count==0) {
[multiplePinArray addObject:propertyObject2];
propertyObject1IsMultiple = YES;
[_forSaleMultiplePropertiesArray addObject:multiplePinArray];
}else{
BOOL propFound = NO;
NSMutableArray *propArr;
NSInteger index = -1;
for(NSMutableArray *arr in _forSaleMultiplePropertiesArray){
if (![arr containsObject:propertyObject2]&&!propFound) {
[arr addObject:propertyObject2];
propertyObject1IsMultiple = YES;
propFound = YES;
index = [_forSaleMultiplePropertiesArray indexOfObject:arr];
propArr = [[NSMutableArray alloc]initWithArray:arr];
}
}
if (propArr!=nil) {
[_forSaleMultiplePropertiesArray replaceObjectAtIndex:index withObject:propArr];
}
}
}
}
if (!propertyObject1IsMultiple) {
[_forSaleSinglePropertiesArray addObject:propertyObject1];
}
}
}
}
答案 0 :(得分:1)
好的......
我将此作为占位符。
按PARCELID排序父数组。
迭代数组。
分为两堆。
......或者其他什么。稍后会写。
答案 1 :(得分:0)
我会更喜欢这样做:
- (void)solution {
self.forSaleSinglePropertiesArray = [[NSMutableArray alloc] init];
self.forSaleMultiplePropertiesArray = [[NSMutableArray alloc] init];
NSDictionary *partitionedProperties = partitionPropertiesByParcelID(self.forSalePropertiesArray);
[self dividePropertiesIntoSingleAndMultiple:partitionedProperties];
}
NSDictionary *partitionPropertiesByParcelID(NSArray *properties) {
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
for (NSDictionary *property in properties) {
id parcelID = property[PARCEL_ID];
NSMutableArray *parcels = result[parcelID];
if (parcels == nil) {
parcels = [[NSMutableArray alloc] init];
[result setObject:parcels forKey:parcelID];
}
[parcels addObject:property];
}
return result;
}
- (void)dividePropertiesIntoSingleAndMultiple:(NSDictionary *)partitionedProperties {
for (NSArray *properties in partitionedProperties.allValues) {
if (properties.count == 1) {
[self.forSaleSinglePropertiesArray addObject:properties[0]];
}
else {
assert(properties.count > 1);
[self.forSaleMultiplePropertiesArray addObject:properties];
}
}
}
首先,代码创建一个Dictionary,其中键是parcel ID,值是具有该parcle ID的属性数组。然后它通过该字典并将每个宗地ID的代表放入单个或多个数组中。
我觉得这段代码更容易理解,我强烈怀疑如果你对上面的代码和答案做了性能指标,那么这段代码将比大型数据集具有更好的性能。我相信这是真的,因为你的答案似乎有O(n ^ 2)表现而我的是O(n)。 (如果您不确定这意味着什么,请阅读“Big-O表示法”。)
此外,您确定您的答案实际适用于所有数据集吗?在迭代它时从数组中删除对象会在我的书中引发一个巨大的红旗。
答案 2 :(得分:0)
我非常喜欢@Fogmeister的那种想法,我实现了它:
- (void)sortSolultion {
self.forSaleSinglePropertiesArray = [[NSMutableArray alloc] init];
self.forSaleMultiplePropertiesArray = [[NSMutableArray alloc] init];
NSArray *forSaleArray = [self.forSalePropertiesArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1[PARCEL_ID] compare:obj2[PARCEL_ID]];
}];
id currentParcelID = nil;
self.propertiesWithCurrentParcelID = [[NSMutableArray alloc] init];
for (NSDictionary *property in forSaleArray) {
if (self.propertiesWithCurrentParcelID.count == 0) {
currentParcelID = property[PARCEL_ID];
}
else if (![property[PARCEL_ID] isEqual: currentParcelID]) {
[self placeCurrentPropertiesInCorrectArray];
currentParcelID = property[PARCEL_ID];
}
[self.propertiesWithCurrentParcelID addObject:property];
}
[self placeCurrentPropertiesInCorrectArray];
}
- (void)placeCurrentPropertiesInCorrectArray {
if (self.propertiesWithCurrentParcelID.count > 1) {
[self.forSaleMultiplePropertiesArray addObject:self.propertiesWithCurrentParcelID];
}
else if (self.propertiesWithCurrentParcelID.count == 1) {
[self.forSaleSinglePropertiesArray addObject:self.propertiesWithCurrentParcelID[0]];
}
[self.propertiesWithCurrentParcelID removeAllObjects];
}
这个解决方案的圈复杂度比我之前的解决方案略高,实际上它比我的第一个解决方案更难以获得它。但该解决方案的分配占用空间较小。两者似乎都具有相同的大O复杂性。
答案 3 :(得分:-1)
好吧,我自己做对了。
将代码更新为更简单快捷的机制:
if (_forSaleSinglePropertiesArray==nil) {
_forSaleSinglePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSaleMultiplePropertiesArray==nil) {
_forSaleMultiplePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSalePropertiesArray!=nil) {
NSMutableDictionary *samePropsDict = [[NSMutableDictionary alloc]init];
for (NSDictionary* dict in _forSalePropertiesArray) {
NSMutableArray *samePropsArray = [samePropsDict objectForKey:[dict valueForKey:PARCEL_ID]];
if (samePropsArray==nil) {
samePropsArray = [[NSMutableArray alloc]init];
}
[samePropsArray addObject:dict];
[samePropsDict setObject:samePropsArray forKey:[dict valueForKey:PARCEL_ID]];
}
for (NSString *key in [samePropsDict allKeys]) {
NSArray *arr = [samePropsDict objectForKey:key];
if (arr.count>1) {
[_forSaleMultiplePropertiesArray addObject:arr];
}else{
[_forSaleSinglePropertiesArray addObject:[arr firstObject]];
}
}
}