首页 > 其他 > 详细

Leetcode - 46. 全排列

时间:2021-09-07 16:16:54      阅读:8      评论:0      收藏:0      [点我收藏+]

给定一个不含重复数字的数组nums,返回其所有可能的全排列。你可以按任意顺序返回答案。

示例 1:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

示例 2:

输入:nums = [0,1]
输出:[[0,1],[1,0]]

示例 3:

输入:nums = [1]
输出:[[1]]

提示:

  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • nums 中的所有整数 互不相同

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解1 2021/9/7 O(?)

from itertools import permutations

def permute(nums: list) -> list:
    res=[]
    for p in permutations(nums):
        res.append(list(p))
    return res

if __name__ == ‘__main__‘:
    print(permute([1,2,3]))
    print([0,1])
    print([1])

技术分享图片

Leetcode - 46. 全排列

原文:https://www.cnblogs.com/Code2235/p/15237718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!