TikTok 面试真题解析:一维下标转多维坐标(Flatten Index → Coordinates)|数组与索引映射基础题

31次阅读
没有评论

Given shape D: [d0, d1, d2, ... dm-1] and a 1-d index, output the corresponding coordinates.

inputs:
shape: (2, 3)
1-d index: 3output:
[1, 0]

This problem asks you to convert a flattened one-dimensional index back into its corresponding multi-dimensional coordinates based on the given shape. The key idea is to understand how multi-dimensional arrays are stored in row-major order. By repeatedly dividing and taking remainders using the dimension sizes from the last dimension backward, you can reconstruct each coordinate position. It essentially mirrors how array indexing works internally.

这道题本质是在考察“多维数组如何映射到一维”的原理。通常数组按行优先(row-major order)展开,因此可以通过不断对 index 进行整除和取余运算,从最后一维开始反推每一维的坐标值。理解这一点,就相当于理解了多维数组底层的存储方式。

正文完
 0