leetcode学习其他人的代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012282037/article/details/73431057

最近刷leetcode时发现了一个比较好的学习方法,

accept后,可以查看其他人的代码,对比自己的,可以学到很多精妙的思路。

例如,这道题目,求出一个列表的所有子列表:

If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
我是用组合来写的:

import itertools
class Solution(object):
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result=[]
        length=len(nums)
        for i in range(length+1):
            result+=list(map(list,itertools.combinations(nums,i)))
        return result

看了别人的代码后,又学习到了一种新的方法:



没有对比就没有伤害,这样学习效率是非常高的。

猜你喜欢

转载自blog.csdn.net/u012282037/article/details/73431057