我正在寻找一种算法来解决以下问题:
我有一组数字
(e.g 100,74,104,76,29,79,98,33,201)
我想对彼此相邻的数字进行分组(相差 x)
例如 x=10 应该输出:
[(100,104,98) (74,76,79) (33,29) (201)]
不幸的是,我不知道该怎么做。
编辑:我有很多开始的想法。该算法不一定要高效,只要工作就可以了。
其中之一是:
- A) Picking first number, comparing its size with all the other numbers
- B) If the condition is complied, saving it in another set and deleting it from the input set
- C) Select the next element that isn't deleted and Start at A (Proceed until input set is empty)
你怎么看?