#94 按下标插入


  • 0

    @elfish#94 按下标插入 中说:

    con牛逼了哥们


  • 0

    @macro 他讲的意思应该是items里面每个元素变成一个只有一个元素的数组,items = [['item1'],['item2'],['item3'],['item4'],['item5']],然后按照另外一个数组需要插入的index,往对应的子数组里面丢数据比如items[0].unshift('section1');items[2].unshift('section2');然后的话就是const results = Array.prototype.concat.apply([], items) // => ['section1', 'item1', 'item2', 'section2', 'item3', 'item4', 'item5']


  • 0

    const injectSections = (items, sections) => {
    let addCount = 0 ; // 记录插入了多少个,下次插入的位置要递增该数值
    sections.sort((a,b)=>{return a.index-b.index}) ; // arr2排序,确保根据index先后来插入
    sections.forEach((val)=>{
    items.splice(val.index+addCount++,0,val.content) ; // 插入值,插入的位置由index和addCount共同决定
    })
    return items ;
    }


  • 0

    这是我能想到的方法

    const injectSections = (items, sections) => {
    
       sections.sort(function (x, y) {
           return x.index > y.index ? 1 : -1;
       });
    
       let num = 0;
       for (let i in sections) {
           items.splice(sections[i].index + num, 0, sections[i].content);
           num++;
       }
       return items;
    };

  • 0

    我的原理是在每次插入时,计算当前位置的左边已插入(p.index < c.index && p.inserted == true)的对象数量,然后进行相对移位插入。

    const injectSections = (items, sections) => {
      var items = [].concat(items);
      var sections= [].concat(sections);
      sections.forEach(c => {
        let num = sections.filter(p => (p.index < c.index && p.inserted == true)).length;
        items.splice(c.index + num,0,c.content)
        c.inserted = true
      })
      return items
    } 
    

  • 1

    const injectSections = (items, sections) => {
    sections.forEach(sectionObj => {
    items[sectionObj.index] = [sectionObj.content,items[sectionObj.index]]
    })
    if ( items.length == 0 ) return [];
    return items.toString().split( ',' )
    }

    思路: 先搞成嵌套的数组 , 然后就转化为拍平数组的问题了


  • 0

    const injectSections = (items, sections) => /* TODO */
    {
      sections.sort(function(cur,next){
        return next['index'] - cur['index']
      });
      for(var i=0;i<sections.length;i++){
        items.splice(sections[i].index,0,sections[i].content);
      }
      return items;
    }
    

  • 0

    const injectSections = (items = [], sections = []) => {
    sections.sort((x, y) => x.index - y.index);
    sections.forEach((x, index) => {
    items.splice(x.index + index, 0, x.content);
    });
    return items;
    };


  • 0

    /**
     * 思路:
     * (用了个小技巧)把原列表中每个item的index加上0.1
     * 再把全部元素从小到大排序。这样子就能确保本来index相同时,新列表的元素总会在前面了。
     */
    
    const injectSections = (items, sections) => {
      // 把原列表中的每一个index加0.1
      const oldList = items.map((item, index) => ({
        content: item,
        index: index + 0.1
      }))
      return oldList.concat(sections).sort((item1, item2) => item1.index - item2.index).map(item => item.content)
    }
    

  • 0

    const injectSections = (items, sections) => {
      const copyAry = Array.prototype.slice.call(items, 0);
      
      for (let i = 0; i < sections.length; i++) {
        const item = sections[i];
        let curIndex = items.indexOf(copyAry[item.index]);
        items.splice(curIndex, 0, item.content);
      }
      
      return items;
    }
    

  • 0

    看了很多答案……怎么都直接改变参数了

    Immutable is better

    const injectSections = (items, sections) => {
        const cloneItems = [...items];
        const cloneSections = [...sections];
    
        cloneSections
            .sort((a, b) => b.index - a.index)
            .forEach(item => cloneItems.splice(item.index, 0, item.content))
    
        return cloneItems;
    
    }
    

  • 0

    主要就两种方案吗, 期待有不一样的脑回路

    倒排方案
    const injectSections = (items, sections) => {
    sections.sort((a,b)=>b-a).forEach(item => items.splice(item.index,0,item.content))
    return item
    }
    concat 方案
    const injectSections = (items, sections) => {
    sections.forEach(section => items[section.index] = [section.content, items[section.index]])
    return [].concat(...items)
    }


  • 0

    const injectSections = (items, sections) => {
      if(items.length===0) return items;
      sections.forEach((item,index)=>{
        items[item.index]=item.content+' '+items[item.index];
      })
      return items.join(' ').split(' ');
    }
    

  • 0

    @teeeemoji
    const injectSections = (items, sections) => {
    //解法1 插空法
    var arr=[];
    // 五个数填六个空,共11次循环
    for(var i=0;i<items.length*2+1;i++){
    // 奇数时插入items
    if(i%2==1){arr.push(items[Math.floor(i/2)])}
    else arr.push(null)
    }
    //插入sections
    for(let i=0;i<sections.length;i++){
    arr[sections[i].index*2]=sections[i].content;
    }
    arr=arr.filter(i=>{
    if(i!=null) return i
    })
    return arr
    }
    第一次写出来的想法= = 感觉是公考数学题思维哈哈哈...


  • 0

    const injectSections = (items, sections) => {
    sections.sort((a, b) => a.index - b.index).forEach((item, index) => items.splice(item.index+index, 0, item.content));
    return items;
    }

    会改变原数组


  • 0

    排个序 然后每循环一项往后挪一个插入就好啦
    sections.sort((a,b) => {
    return a.index - b.index
    });
    sections.forEach((ele,index) => {
    items.splice(ele.index + index,0,ele.content)
    })
    return items


  • 0

    @GGGGrey 顺排法了解一下


  • 0

    const injectSections = (items, sections) => {
    let res = items.reduce((p, i, index) => {
    sections.forEach(section => {
    if (section.index === index) {
    p[index] = items[index]
    }
    })
    return p
    }, {})
    sections.forEach((section, i) => {
    const {content, index} = section
    let findIndex = items.findIndex(i => i === res[index])
    if (findIndex !== -1) {
    items.splice(findIndex, 0, content)
    }
    })
    return items
    }


登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待