简体   繁体   English

获取 n 维数组中的下一个元素

[英]get next element in n-dimensional array

I was given an interview question that I just could not solve.我被问到一个我无法解决的面试问题。 I feel like the solution was recursive, but even then I couldn't sort through the logic.我觉得解决方案是递归的,但即便如此,我也无法理清逻辑。 The question was as follows问题如下

Given nested arrays, write an enumerator class such that next() returns the elements in sequential order, eg:给定嵌套数组,编写一个枚举类,使 next() 按顺序返回元素,例如:

So if the input is: [1,[4,3],6,[5,[1,0]]] The output is : 1, 4, 3, 6, 5, 1, 0所以如果输入是:[1,[4,3],6,[5,[1,0]]] 输出是:1, 4, 3, 6, 5, 1, 0

I was supposed to do it in Objective-C and my (bad solution) was我应该在 Objective-C 中做到这一点,而我的(糟糕的解决方案)是

- (void) getNext(NSArray *) array {

  while(!array.isEmpty)
    getNext(array objectAtIndex(first);
}

NSLog(@"object is %@", [array objectAtIndex[0]]);

Anyone have any ideas?谁有想法? I would also accept a non-objective-C answer just to see the logic我也会接受一个非客观的 C 答案只是为了看看逻辑

I created a NSArray category.我创建了一个NSArray类别。

@interface NSArray(EXT)
@property (nonatomic, assign) NSNumber *index;
- (NSNumber *)next;
@end

static void *aIndex = &aIndex;
@implementation NSArray (EXT)
    - (NSNumber *)index {
    return objc_getAssociatedObject(self, aIndex);
}

- (void)setIndex:(NSNumber *)index {
    objc_setAssociatedObject(self, aIndex, index, OBJC_ASSOCIATION_ASSIGN);
}

- (NSNumber *)next {
    if(self.index.integerValue == self.count) {
        return nil;
    }
    else {
        id nextObj = [self objectAtIndex:self.index.integerValue];
        if([nextObj isKindOfClass:[NSNumber class]]) {
            self.index = @(self.index.integerValue+1);
            return nextObj;
        }
        else {
            id childNext = [(NSArray *)nextObj next];
            if(childNext == nil) {
                self.index = @(self.index.integerValue+1);
                return [self next];
            }
            else {
                return childNext;
            }
        }
    }
}
@end

and you can use this like as below:你可以像下面这样使用它:

NSArray *input = @[@1, @[@4, @3], @6, @[@5, @[@1, @0]]];

NSNumber *next = [input next];
while (next != nil) {
    NSLog(@"%@", next);
    next = [input next];
}

The simple solution to this problem is这个问题的简单解决方案是

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *finalArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *input = @[@1, @[@4, @3], @6, @[@5, @[@1, @0]]];
    _finalArray = [NSMutableArray array];

    [self arrayToString:input];

    NSLog(@"Final array %@", [self.finalArray componentsJoinedByString:@","]);
}


- (void)arrayToString: (NSArray *) array {
    for (int i = 0; i < [array count]; i++) {
        [self recurseValue:[array objectAtIndex:i]];
    }


}


- (void)recurseValue:(id)value {
    if ([value isKindOfClass:[NSNumber class]]) {
        [self.finalArray addObject:value];
    } else if ([value isKindOfClass:[NSArray class]]) {
        for (int i = 0; i< [value count]; i++) {
            [self recurseValue:[value objectAtIndex:i]];
        }
    }
}

Output : 1,4,3,6,5,1,0输出:1,4,3,6,5,1,0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM