Recentemente ho dovuto risolvere la seguente domanda dell'algoritmo, che mi ha confuso.Restituisce tutti i set in cui tutti i relativi numeri non sono in nessun altro set
Supponiamo di disporre di un array di insiemi che contengono numeri interi. Scrivi una funzione che restituisce tutti i set in cui tutti i suoi numeri non sono in nessun altro set .
ESEMPIO {0,4,9}, {3,4,5}, {6,7,8}
RESULT {6,7,8}
Il codice dovrebbe essere in Objective-C o Swift.
[EDIT]
mi è venuta finora con qualcosa di simile, ma non può davvero finire.
- (NSArray*) getDisjointedSets:(NSArray*)sets {
NSArray* resultedSet;
NSMutableArray* indexDoesntExistInSets = [NSMutableArray array];
[sets enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSArray* nextIndexArray = [sets objectAtIndex:idx+1];
NSNumber* numberIndex = obj;
[nextIndexArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSNumber* numberNextIndex = obj;
if (numberIndex.intValue == numberNextIndex.intValue) {
// number exists in the next set, continue
*stop = YES;;
} else {
[indexDoesntExistInSets addObject:numberIndex];
}
}];
}];
return resultedSet;
}
Cosa c'è si mette in discussione? Che cosa hai provato? Che problema stai avendo? O vuoi solo che qualcuno ti dia il codice? – rmaddy
Questa è la domanda sopra, non ho potuto risolverlo, quindi cerco la risposta per capirlo. –
Pubblica ciò che hai provato e spiega quale problema stai riscontrando. – rmaddy