Objective-C - 将数字数组转换为数字依次排列的数组

时间:2012-11-27 12:47:16

标签: objective-c cocoa numbers sequence

是否有一种简单的方法可以将数字数组转换为数字顺序的数组?

NSArray *numbers = @[@1,@2,@5,@3];

// Transformed arrays
//NSArray *numbersInSequence = @[@1,@2,@3];
//NSArray *numbersInSequence2 = @[@5];

修改

我修改了Richard's answer中的代码以使其正常工作。

NSArray *arraysBySplittingNumbersInOrder(NSArray *input) {

// sort 'input'
input = [input sortedArrayUsingSelector:@selector(compare:)];

NSMutableArray *results = [NSMutableArray array];

if (input.count) {

    int start = 0;
    int last = INT_MIN;

    for (int i = 0; i < input.count; i++) {

        BOOL lastItem = i == input.count - 1;

        // The first item of the array
        if (i == 0) {

            if (lastItem) {
                [results addObject:input];
                break;
            }

            last = [input[i] intValue];
            continue;
        }

        int cur = [input[i] intValue];

        if (cur != last + 1) {

            // pull out the next array
            [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start)]];

            start = i;
        }

        // The last item of the array
        if (lastItem) {

            [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start + 1)]];            
        }

        last = cur;
    }
}

return results;
}

2 个答案:

答案 0 :(得分:0)

我认为没有 easy 方法可以做到这一点;你可能必须自己完成至少部分工作。

我的建议是对数组进行排序,然后遍历它,随时构建各个部分。每当你点击“跳跃”,即一个非连续的数字时,你的当前部分结束并开始一个新的。

答案 1 :(得分:0)

这是一个相当简单的解决方案:

NSArray *arraysBySplittingNumbersInOrder(NSArray *input)
{
    // sort 'input'
    input = [input sortedArrayUsingSelector:@selector(compare:)];

    NSMutableArray *results = [NSMutableArray array];

    if (input.count)
    {
        int start = 0;
        int last = INT_MIN;

        for (int i = 0; i <= input.count; i++)
        {
            if (i == 0)
            {
                last = [input[i] intValue];
                continue;
            }
            if (i == input.count)
            {
                if (i != start + 1)
                {
                    [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start)]];
                    continue;
                }
            }

            int cur = [input[i] intValue];

            if (cur != last + 1)
            {
                // pull out the next array
                [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start)]];
                start = i;
            }

            last = cur;
        }
    }

    return results;
}

int main()
{
    NSArray *input = @[ @1, @3, @4, @7, @8, @12, @13, @14 ];

    NSLog(@"%@", input);
    NSLog(@"%@", arraysBySplittingNumbersInOrder(input));
}

输出:

2012-11-27 07:55:04.609 TestProj[35890:303] (
    1,
    3,
    4,
    7,
    8,
    12,
    13,
    14
)
2012-11-27 07:55:04.611 TestProj[35890:303] (
        (
        1
    ),
        (
        3,
        4
    ),
        (
        7,
        8
    ),
        (
        12,
        13,
        14
    )
)