Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Permutations problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Permutations problem solution, we have given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Leetcode Permutations problem solution

Leetcode Permutations problem solution in Python.

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:])]

Permutations problem solution in Java.

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> ds = new ArrayList<>();
        boolean[] freq = new boolean[nums.length];
        fun(nums, ds,ans, freq);
        return ans;
    }
    public void fun(int[] nums,List<Integer> ds,List<List<Integer>> ans,boolean[] freq){
        if(ds.size()==nums.length){
            ans.add(new ArrayList<>(ds));
            return ;
        }
        for(int i=0;i<nums.length;i++){
            if(!freq[i]){
                freq[i] = true;
                ds.add(nums[i]);
                fun(nums, ds,ans, freq);
                ds.remove(ds.size()-1);
                freq[i] = false;
            }
        }
    }
}

Problem solution in C++.

class Solution {
public:
    vector<vector<int>> result;
    
    void dfs(vector<int> &nums, int n){
        
        if(n==nums.size()) {
            result.push_back(nums);
            return;
        }
        else{
            for(int i=n;i<nums.size();i++){
                swap(nums[i],nums[n]);
                dfs(nums,n+1);
                swap(nums[i],nums[n]);
            }
        }
    }
    
    vector<vector<int>> permute(vector<int>& nums) {
        dfs(nums,0);
        return result;
    }
};

Problem solution in C.

void permute_step(int* p, int n, int* k,int** rp,int nn)
{
    int tmp;
    if (n == 1)
    {
        rp[*k][n - 1] = p[0];
        for (int i = 0;i < nn;i++)
        {   
            if (rp[*k][i] <= -1024) rp[*k][i] = rp[(*k) - 1][i];
        }
        (*k)++;
    }
    else
    {
        for (int i = 0;i < n;i++)
        {
            rp[*k][n - 1] = p[i];tmp = p[i];p[i] = p[0];
            permute_step(p + 1, n - 1, k, rp, nn);
            p[0] = p[i];p[i] = tmp;
        }
    }
}

int** permute(int* nums, int numsSize, int* returnSize) {
    *returnSize = 1;
    for (int i = 1;i <= numsSize;i++)
        *returnSize *= i;
    int** r = (int**)malloc(sizeof(int*)*(*returnSize));
    for (int i = 0;i < *returnSize;i++)
    {
        r[i] = (int*)malloc(sizeof(int)*numsSize);
        for (int j = 0;j < numsSize;j++)
            r[i][j] = -1024;
    }
    int k = 0;int* kp = &k;
    permute_step(nums, numsSize, kp, r, numsSize);
    return r;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes