You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

itertools.go 513B

123456789101112131415161718192021
  1. package common
  2. // Permutations returns all possible permutations of the given ints.
  3. // Source: https://github.com/dolotech/datastructures-algorithms/blob/master/NUMERICSLICEPERMUTATIONGENERATOR.md
  4. func Permutations(xs []int) (res [][]int) {
  5. var rc func([]int, int)
  6. rc = func(a []int, k int) {
  7. if k == len(a) {
  8. res = append(res, append([]int{}, a...))
  9. } else {
  10. for i := k; i < len(xs); i++ {
  11. a[k], a[i] = a[i], a[k]
  12. rc(a, k+1)
  13. a[k], a[i] = a[i], a[k]
  14. }
  15. }
  16. }
  17. rc(xs, 0)
  18. return res
  19. }