Short C# solution - beats 96%


  • 0
    N
    public class Solution {
        public IList<IList<int>> perms = new List<IList<int>>();
        public List<int> perm = new List<int>();
        public IList<IList<int>> Permute(int[] nums) 
        {
            GetPerms(perm, nums);
            return perms;
        }
        public void GetPerms (List<int> perm, int[]nums)
        {
            if(perm.Count == nums.Length)
            {
                perms.Add(new List<int>(perm));
                return;
            }
            
            for(int i = 0; i < nums.Length; i++)
            {
                if(!perm.Contains(nums[i]))
                {
                    perm.Add(nums[i]);
                    GetPerms(perm, nums);
                    perm.Remove(perm.Last());
                }
            }
            return;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.