Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. public class Solution {
  2.    
  3.     public IList<IList<int>> Permute(int[] values)
  4.     {
  5.         return Perm(values);
  6.     }
  7.  
  8.     public IList<IList<int>> Perm(int[] values)
  9.     {
  10.         if (values.Length == 1)
  11.             return new List<IList<int>>(new[] { values });
  12.  
  13.         else
  14.         {
  15.             return values
  16.                 .SelectMany((value, index) =>
  17.                 {
  18.                     var primary = new[] { value };
  19.                     return this.Perm(Splice(values, index))
  20.                         .Select(perm =>
  21.                         {
  22.                             return primary.Concat(perm).ToList() as IList<int>;
  23.                         });
  24.                 })
  25.                 .ToList();
  26.         }
  27.     }
  28.  
  29.     public int[] Splice(int[] list, int index)
  30.     {
  31.         if (index >= list.Length)
  32.             throw new Exception();
  33.  
  34.         else return list
  35.             .Take(index)
  36.             .Concat(list.Skip(index + 1))
  37.             .ToArray();
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement