當(dāng)前位置:
首頁 >
Divide a list of numbers into group of consecutive numbers
發(fā)布時間:2024/9/30
39
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Divide a list of numbers into group of consecutive numbers
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//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中的值,這肯定是有序的,就可以將有序的數(shù)字分組。
更為直接的方法就是,先對數(shù)組排序,即可
有更快的算法:
定義一個bool a[n],記錄 數(shù)組中的數(shù)字是否被處理過。
用map<int,int>記錄key 和 index的值
從左到右依次處理在a中表明未被訪問過的數(shù)字,并-1或者+1向左右擴展,通過查詢map來確定數(shù)組是否存在,并將a中對應(yīng)的下標(biāo)設(shè)為已訪問。
總結(jié)
以上是生活随笔為你收集整理的Divide a list of numbers into group of consecutive numbers的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开源日志系统
- 下一篇: vs2008 使用Visual Leak