zoukankan      html  css  js  c++  java
  • 一道很有纪念意义的算法题之多维数组去重,不降维

    背景:很久之前的压轴面试算法题。

    解决方案:

    // 多维排重
    let x = [123, [3,4, [523,3, 1], [3,4,67]]];
    //结果 [123, [3,4, [523, 1], [67]]];

    const stant = x.flat(Infinity)
    const newArr = Array.from(new Set(stant))
    let Ar = []
    function fof(arr){
        for(let i=0;i<arr.length;i++){
            if(Array.isArray(arr[i])){
                fof(arr[i])
            }else{
                if(Ar.indexOf(arr[i]) !== -1){
                    arr.splice(i,1)
                    i--
                }else{
                    Ar.push(arr[i])
                }
            }  
        }
    }
    fof(x)
    console.log(newArr)
    console.log(x)
    console.log(Ar)
    现在看来结果其实不重要了,最近也是闲下来去做做算法题偶然间想起了之前的一道没有答出来的题,希望能给大家新的思路
  • 相关阅读:
    H
    并查集
    H
    Supermarket (贪心 并查集)
    H
    M
    N-Find a way
    HDU 4035 Maze
    #386. 【UNR #3】鸽子固定器
    P4688 [Ynoi2016]掉进兔子洞
  • 原文地址:https://www.cnblogs.com/MDGE/p/14201862.html
Copyright © 2011-2022 走看看