例子 已知 起點(diǎn)為0 末點(diǎn)為80000arr [0] :起點(diǎn)arr [1] :末點(diǎn)起點(diǎn)與末點(diǎn)如果相同要求進(jìn)行合并最終產(chǎn)生新的剩余段落以下的數(shù)組為完成的線段 arr =
[0, 0],//起
[500, 1000],
[3000, 3500], //相同1
[6000, 10000],//相同2
[3500, 5000],//相同1
[10000, 20000],//相同2
[50000, 80000]//止轉(zhuǎn)換為 [0, 500], [1000, 3000], [5000, 6000],
[20000, 50000],
2 回答

慕后森
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個贊
var arr =[ [0, 0],//起 [500, 1000], [3000, 3500], //相同1 [6000, 10000],//相同2 [3500, 5000],//相同1 [10000, 20000],//相同2 [50000, 80000]//止] arr.sort((a,b)=>a[0] - b[0])let point = 0;let res = [];for (let index = 0; index < arr.length; index++) { if(point < arr[index][0]){ res.push([point,arr[index][0]]) point = arr[index][1] }else{ point = Math.max(point,arr[index][1]) } } console.log(res)
樓上說的對,時間復(fù)雜度取決于排序
- 2 回答
- 0 關(guān)注
- 459 瀏覽
添加回答
舉報
0/150
提交
取消