Code Signal_10分钟挑战题_constructArray

完成时间3分06秒

Given an integer size, return an array containing each integer from 1 to size in the following order:

1, size, 2, size - 1, 3, size - 2, 4, ...

Example

For size = 7, the output should be
constructArray(size) = [1, 7, 2, 6, 3, 5, 4].

我的解答:

def constructArray(size):
    l = []
    x = 1
    for i in range(size):
        if i % 2 == 0:
            l.append(x)
            x += 1
        else:
            l.append(size+2-x)
    return l

猜你喜欢

转载自www.cnblogs.com/YD2018/p/9465469.html