Permutations LeetCode Python

LeetCode has a Medium coding Problem in Its’ Algorithm Section “Permutations LeetCode Python”. Today We are going to solve this problem in Python. LeetCode Link of the Problem is HERE


Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.


Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Input: nums = [0,1]
Output: [[0,1],[1,0]]

Input: nums = [1]
Output: [[1]]


  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • All the integers of nums are unique.

Solution to Permutations LeetCode Python

class Solution(object):
def permute(self, nums):

  • if not nums:
    • return [[]]
  • return [[nums[i]] + j for i in xrange(len(nums)) for j in self.permute(nums[:i]+nums[i+1:])]


Runtime: 44 ms, faster than 25.45% of Python online submissions for Permutations.

Memory Usage: 13.5 MB, less than 86.72% of Python online submissions for Permutations.


Python-related posts Visit HERE

C++ related posts Visit HERE

Databases related posts Visit HERE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge