#94 按下标插入


  • 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 的连接断开,我们正在尝试重连,请耐心等待