Divide a list of numbers into group of consecutive numbers
生活随笔
收集整理的這篇文章主要介紹了
Divide a list of numbers into group of consecutive numbers
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//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中對應的下標設為已訪問。
總結
以上是生活随笔為你收集整理的Divide a list of numbers into group of consecutive numbers的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开源日志系统
- 下一篇: C++——auto_ptr类