aboutsummaryrefslogtreecommitdiff
path: root/permutations.c
blob: 38caf6956db1e9ccec26a8d1d6d3e77d5f2d5d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 46. Permutations
#include <stdio.h>
#include <stdlib.h>

/*
 * given an array 'nums' of distinct integers, return all the possible
 * permutations. you can return the answer in any order
 */

void swap(int *p, int *q) {
  int tmp = *p;
  *p = *q;
  *q = tmp;
}

void search(int *nums, int nums_size, int ***arr, int *return_size, int begin,
            int end) {
  if (begin == end) {
    (*return_size)++;
    *arr = (int **)realloc(*arr, sizeof(int *) * (*return_size));
    (*arr)[*return_size - 1] = (int *)malloc(sizeof(int) * nums_size);
    for (int i = 0; i < nums_size; i++)
      (*arr)[*return_size - 1][i] = nums[i];
    return;
  }
  for (int i = begin; i <= end; i++) {
    swap(nums + begin, nums + i);
    search(nums, nums_size, arr, return_size, begin + 1, end);
    swap(nums + begin, nums + i);
  }
}

int **permute(int *nums, int nums_size, int *return_size,
              int **return_col_size) {
  *return_size = 0;
  int **ans = (int **)malloc(sizeof(int *));
  search(nums, nums_size, &ans, return_size, 0, nums_size - 1);
  return ans;
}

int main() {
  int n1[] = {1, 2, 3}, n2[] = {0, 1}, n3[] = {1};
  int **p1 = permute(n1, 3, NULL, NULL);
  int **p2 = permute(n2, 2, NULL, NULL);
  int **p3 = permute(n3, 1, NULL, NULL);
  for (int i = 0; i < 6; i++)
    for (int j = 0; j < 3; j++)
      printf("%d ", p1[i][j]);
  printf("\n");
  for (int i = 0; i < 6; i++)
    for (int j = 0; j < 3; j++)
      printf("%d ", p2[i][j]);
  printf("\n");
  for (int i = 0; i < 6; i++)
    for (int j = 0; j < 3; j++)
      printf("%d ", p3[i][j]);
  printf("\n");
}