public void nextPermutation(int[] num) {

    int partitionIdx = -1; // default as 0
    for (int i = num.length - 2 ; i >= 0 ; i--) {
      if (num[i] < num[i + 1]) {
        partitionIdx = i;
        break;
      }
    }


    if (partitionIdx >= 0) {
      for (int i = num.length - 1 ; i > partitionIdx ; i--) {
        if (num[i] > num[partitionIdx]) {

          int temp = num[i];
          num[i] = num[partitionIdx];
          num[partitionIdx] = temp;
          break;
        }
      }
    }

    int head = partitionIdx + 1;
    int tail = num.length - 1;
    while (head < tail) {
      int temp = num[head];
      num[head] = num[tail];
      num[tail] = temp;
      head++;
      tail--;
    }
  }