现在的位置: 首页 > 综合 > 正文

Divide a list of numbers into group of consecutive numbers

2013年12月05日 ⁄ 综合 ⁄ 共 369字 ⁄ 字号 评论关闭

//Divide a list of numbers into group of consecutive numbers but their original order should be preserved?
//8,2,4,7,1,0,3,6
//2,4,1,0,3 and 8,7,6

//obviously in shortest time and space.

我想到的方法比较土,用set存储所有的值。用iteration 指针变量set中的值,这肯定是有序的,就可以将有序的数字分组。

更为直接的方法就是,先对数组排序,即可

有更快的算法

定义一个bool a[n],记录 数组中的数字是否被处理过。

用map<int,int>记录key 和 index的值

从左到右依次处理在a中表明未被访问过的数字,并-1或者+1向左右扩展,通过查询map来确定数组是否存在,并将a中对应的下标设为已访问。

抱歉!评论已关闭.