2014-02-08 4 views
-2

첫 번째 배열에 대해 크기 4가 부여됩니다. (이것은 테스트 케이스에 따라 다를 것이다).permuations를 int 배열에 넣는 방법

는 INT []의 요소

순열을하고있는 int [] []로 넣어 어떤 알고리즘 또는 수식있다 {1, 3, 3, 4}을 갖는다 말하게? 예를 들어

: 첫 번째 배열의 크기가 항상되지 않습니다 3.
a[0][] = {1, 3, 3}
a[1][] = {1, 3, 4}
a[2][] = {1, 3, 4}
a[3][] = {3, 3, 4}

의 그룹에서 위의 배열의 순열도 염두에 두어야 4,하지만 그것은 항상 3의 그룹으로 될 것입니다.

기본적으로 int []의 순열을 anothe r int [] []

+0

어려운 이해. –

+0

미안 해요, 기본적으로 int []의 순열을 다른 int [] []에 넣어야합니다. – XT1shX

+0

https://stackoverflow.com/questions/20906214/permutation-algorithm-for-array-of-integers-in-java가 질문에 대한 답변에 가까울 것 같습니다. ("the"를 " "의심 할 여지없이 여러 가지 좋은 솔루션이 있기 때문에" – keshlam

답변

0

마지막으로 입력 배열의 모든 크기에 대해 permutation 알고리즘을 구현했습니다. 그것은 여기에, 흥미로웠다는 것입니다 :

import java.util.Arrays; 

public class PermutationCalculator { 
    public static void main(String[] args) { 
     final int[] input = {1, 3, 3, 4}; 
     int[][] result = new PermutationCalculator().permutation(input); 

     // print result 
     for (int i = 0; i < input.length; i++) { 
      System.out.println(Arrays.toString(result[i])); 
     } 
    } 

    public int[][] permutation(int[] input) { 
     int[][] result = new int[input.length][]; // i-th row 
     for (int i = input.length - 1; i >= 0; i--) { 

      // negI starts from 0 instead of i which start from end 
      int negI = input.length - i - 1; 
      result[negI] = new int[input.length - 1]; 

      // j is row input array index, 
      // jj is column index (column length = input array -length - 1) 
      for (int j = 0, jj = 0; jj < input.length; j++, jj++) 
       if (jj == i) { 
        j--; // don't need increasing in this case 
       } else { 
        result[negI][j] = input[jj]; 
       } 
     } 

     return result; 
    } 
} 

출력은 다음과 같습니다

[1, 3, 3] 
[1, 3, 4] 
[1, 3, 4] 
[3, 3, 4] 
+0

감사합니다. – XT1shX

관련 문제