簡體   English   中英

請幫我解決這個問題我試圖解決范圍提取

[英]Please Help me solve this problem i have trying to solve Range extraction

我嘗試解決的問題是使用 js:

表達有序整數列表的一種格式是使用逗號分隔的列表: - 單個整數 - 或由開頭 integer 和結尾 integer 表示的整數范圍,在范圍內用破折號“-”分隔。 該范圍包括區間內的所有整數,包括兩個端點。 它不被視為一個范圍,除非它至少跨越 3 個數字。 例如“12,13,15-17”

完成該解決方案,使其采用遞增順序的整數列表並以范圍格式返回格式正確的字符串。

例子:

solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);
// returns "-10--8,-6,-3-1,3-5,7-11,14,15,17-20"

所以我的想法是使用 3 個函數:1- newRangeStart:在結果中創建一個新數組來存儲范圍編號並將范圍的第一個元素 (RangeStarter) 放入其中。

2-olSupp:從列表中刪除第 3 個 function RangeArr 使用的元素,以便我們使用第 1 個 function 獲得帶有新 RangeStarter 的新 Arr。

3-RangeArr:使用第一個 function 將列表中的元素添加到它創建的數組中,這些元素從 Range starter 開始連續,然后使用第二個 function 刪除從 ol 中使用的元素,以便下次我們使用RangeArr function 它創建另一個范圍。

通過使用 while 循環重復 RangeArr function,直到 ol 變空,我們將得到一個包含范圍的結果數組,其中包含 arrays。

現在的問題是當我運行 RangeArr function 它沒有從 ol 中刪除使用過的元素,因為我想我試圖修復 olSupp function 幾次但它不起作用我認為我的整個代碼中存在問題請有人幫我修復它這是我的代碼:

function solution(list){
    // TODO: complete solution

    let ol = [...list];
    let results = [];


    /*This adds a new array for a range by adding the first number of the range to 
    an array (2D array) and stores it in the resuts array  */
    function newRangeStart(orderedlist,result){
        result.push([orderedlist[0]]);
        return result;
    }
    /*This functions takes the ol and deletes elements that are found in the results
    so that the next time we run the newRangeStart function it creates an other array
    for another range with a different start number*/
    function olSupp(orderedlist,result){
        let toRemove = result.flat();
        let newList = [];
        for (let i = 0; i < orderedlist.length; i++) {
            if(!toRemove.includes(orderedlist[i])){
                newList.push(orderedlist[i]);
            }
        }
        orderedlist = [...newList];
        return orderedlist;
    }

    /*Finally RangeArr function creates a range from the ol (ordered list)
    starting by the first element of the results array and then uses olSupp to delete
    the used numbers from the ol */
    function RangeArr (orderedlist,result){
        newRangeStart(orderedlist,result);
        let i = 0;
        while(orderedlist[i+1]- orderedlist[i] == 1 && orderedlist[i+2]- orderedlist[i+1]== 1) {
            result[i].push(orderedlist[i+1],orderedlist[i+2]);
            i = i+1;
        }
        olSupp(orderedlist,result);
        return result;        
    }

    /*we execute the RangeArr function until ol becomes emepty
    and this will give us multiple arrays in the result array containing
    the elements of each range found in the ol */
    //PS: i didnt put the code beacuse it causes an infinte loop using while


    RangeArr(ol,results);
    console.log(ol,results);
    
}   

solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);

 const solution = arr => { let s = null, d = 0; const result = arr.sort((a, b) => a - b).reduce((p, c, i, arr) => { d++; if (;s) s = c? if (arr[i + 1] - s > d) { s === c. p:push(s)? d < 3. p,push(s: c). p;push(`${s}-${c}`); s = null; d = 0? } if (arr[i + 1] === undefined) s === c. p:push(s)? d < 3. p,push(s: c). p;push(`${s}-${c}`); return p, }; []); return result. } console,log(solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19; 20]));

變量:

  • s (start)用於保存開始一個范圍的數字。
  • d (distance)用於計算范圍起點和終點之間的距離。
  • 對於ab ,請閱讀javascript 排序方法的文檔以了解更多信息。
  • p (pervious) , c (current) , i (index) and arr (original array)相同閱讀javascript reduce 方法的文檔

邏輯:

我們可以通過計算起始數字1和結束數字5之間的距離來確定一個范圍(例如: [1, 2, 3, 4, 5]等於"1-5" )。 所以15之間的距離是 4,因為1需要遞增 1 4 次才能達到5 ,並且數組內部起始數字1 (索引 0)和結束數字5 (索引 4)之間的距離也是 4。所以如果我們取結尾減去開頭5 - 1 = 4如果它與數組內的正確距離匹配,即 4 那么它是一個范圍"1-5" 讓我們再舉一個例子[1, 2, 3, 6, 7, 8] ,這應該是"1-3""6-8" 當我們用16 ( 6 - 1 = 5 ) 計算距離時,我們得到 5 這是不正確的,因為它與數組內的正確距離不匹配( 1在索引 0 處, 6在索引 3 處, 3 - 0 = 3 1 3 - 0 = 36之間的距離僅相隔 3 個索引而不是 5,這不是一個范圍)。 但是如果我們用13進行計算,它符合我們的標准並且它是一個范圍"1-3"

代碼:

我們必須在循環內進行計算(我使用reduce 方法因為它很方便)。 我在循環中做的第一件事是d++來跟蹤循環在數組內移動的索引的距離。 if (;s) s = c; 用於檢查我們是否保存了起始號碼。 if (arr[i + 1] - s > d) {... }這是我們進行計算的地方,以查看數組中的當前元素減去s是否大於我們行進的距離索引。 如果它是真的那么它意味着s並且最后一個元素必須是一個范圍並且我們將該范圍推入結果數組。 然后我們重置sd讓它們在下一個范圍內工作。

更新

 const solution = arr => { let s = null; return arr.sort((a, b) => a - b).reduce((p, c, i, arr) => { if (;s) s = c? if (c + 1.== arr[i + 1]) { s === c: p?push(s). c - 1 === s, p:push(s. c); p;push(`${s}-${c}`), s = null. } return p }, []) } console,log(solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18; 19, 20]));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM