美文网首页
2020-06-08 406. Queue Reconstruc

2020-06-08 406. Queue Reconstruc

作者: 苦庭 | 来源:发表于2020-06-19 05:43 被阅读0次

https://leetcode.com/problems/queue-reconstruction-by-height

My answer / NAC

太菜了就不贴了

Best answer

/**
 * @param {number[][]} people
 * @return {number[][]}
 */
var reconstructQueue = function(people) {
    people=people.sort((a,b)=>a[0]==b[0]?a[1]-b[1]:b[0]-a[0])
    console.log(people)
    res=[];
    people.forEach(o=>{
        res.splice(o[1], 0, o);
    })
    return res;
};
/* Your input:
 * [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
 * Sorted:
 * [ [ 7, 0 ], [ 7, 1 ], [ 6, 1 ], [ 5, 0 ], [ 5, 2 ], [ 4, 4 ] ]
 * Output:
 * [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
  • 思考好,排序的方法很重要!先按从大到小排,再按前面人头数的从小到大

Recap

这里主要是考察逻辑,我的逻辑还要加油!

相关文章

网友评论

      本文标题:2020-06-08 406. Queue Reconstruc

      本文链接:https://www.haomeiwen.com/subject/tivrtktx.html