在 iOS 中以给定的起始位置和间隔将元素插入到二维数组中?
Insert elements into 2D array with given start position and interval in iOS?
示例:
NSArray *arr = ... //[[1,2,3], [4,5], [6,7,8]]
NSIndex startPosition = 1;
NSIndex interval = 3;
NSArray *result = ... //[[1,a,2,3,a], [4,5,a], [6,7,a,8]]
其中 a
- 一些值(可以不同)。
如果它是一维数组,那么任务很简单——我只需在从给定位置开始的每个 N 元素之后插入。但是当我有二维数组时如何处理呢?
已更新
How does it related to iOS?
Mobup SDK,手动插入原生广告。答案将有助于找到将广告插入 table 多个部分的位置。
已更新
startPosition
- 要插入的第一个索引
interval
- 跳过要插入的索引前的 N 个元素
如果是一维数组,那么要插入的索引是:
startPosition, startPosition + interval, startPosition + interval * 2, ...
但它是二维数组,很难描述它的公式 - 看例子。
如果数组是一维的,你说你知道如何解决这个问题。在伪代码中,您将使用类似的东西:
currentIndex = startPosition
while currentIndex < arr.length do
insert item
advance currentIndex by interval
end
要将其扩展到二维数组(数组的数组),您只需将上面的代码嵌套在数组的循环中,例如:
currentIndex = startPosition
for arr in arrayOfArrays
while currentIndex < arr.length do
insert item
advance currentIndex by interval
end
// at this point currentIndex has passed the array, to determine
// first index into next array simply subtract the length of the
// current array
currentIndex -= arr.length
end
HTH
示例:
NSArray *arr = ... //[[1,2,3], [4,5], [6,7,8]]
NSIndex startPosition = 1;
NSIndex interval = 3;
NSArray *result = ... //[[1,a,2,3,a], [4,5,a], [6,7,a,8]]
其中 a
- 一些值(可以不同)。
如果它是一维数组,那么任务很简单——我只需在从给定位置开始的每个 N 元素之后插入。但是当我有二维数组时如何处理呢?
已更新
How does it related to iOS?
Mobup SDK,手动插入原生广告。答案将有助于找到将广告插入 table 多个部分的位置。
已更新
startPosition
- 要插入的第一个索引
interval
- 跳过要插入的索引前的 N 个元素
如果是一维数组,那么要插入的索引是:
startPosition, startPosition + interval, startPosition + interval * 2, ...
但它是二维数组,很难描述它的公式 - 看例子。
如果数组是一维的,你说你知道如何解决这个问题。在伪代码中,您将使用类似的东西:
currentIndex = startPosition
while currentIndex < arr.length do
insert item
advance currentIndex by interval
end
要将其扩展到二维数组(数组的数组),您只需将上面的代码嵌套在数组的循环中,例如:
currentIndex = startPosition
for arr in arrayOfArrays
while currentIndex < arr.length do
insert item
advance currentIndex by interval
end
// at this point currentIndex has passed the array, to determine
// first index into next array simply subtract the length of the
// current array
currentIndex -= arr.length
end
HTH