Find all combinations of a given set of numbers

前端 未结 9 1648
故里飘歌
故里飘歌 2020-12-03 09:30

say I have a set of numbers \'0\', \'1\', \'2\', ..., \'9\'. I want to find all numbers that contain exactly one of each of the numbers in my set.

The problem is: Be

9条回答
  •  猫巷女王i
    2020-12-03 09:37

    You should write a recursive function that loops through the list and every time calls itself with an updated list. This means it needs to create a copy of the list with N-1 elements to pass to the next iteration. For results, you need to append the currently selected number in each iteration.

    string Permutations(List numbers, string prefix)
    {
       foreach (current_number in numbers)
       {
          new_prefix = prefix+"-"+number;
          new_list=make_copy_except(numbers,  current_number)
          if (new_list.Length==0)
               print new_prefix
          else
               Permutations(new_list, new_prefix)
       }
    }
    

提交回复
热议问题