2016年11月9日

406. Queue Reconstruction by Height 的Python解法

研究了好久算法,最后参考了别人的思路,才发现代码可以这么简单……

思路总结:
1. 预排序,h从大到小,k从小到大
2. 依次把每个元素插到新list的k位置

class Solution(object):
    def reconstructQueue(self, people):
        """
        :type people: List[List[int]]
        :rtype: List[List[int]]
        """
        result = []
        
        people.sort(key = operator.itemgetter(1))
        people.sort(key = operator.itemgetter(0), reverse = True)
        
        for p in people:
            result.insert(p[1], p)
            
        return result

这样的思路实现起来,真的很简单啊!

没有评论: