[알고리즘]/자료구조

[자료구조] 순열

쿠릉쿠릉 쾅쾅 2022. 2. 17. 13:05
728x90

 

 

순열 : 서로 다른 n 개 중 r 개를 골라 순서를 고려해 나열한 경우의 수

 

 

📌 코드로 구현

import java.util.Arrays;

class Solution {
	
	static int sum=0;
	
	static int[] arr, result;
	static boolean[] check;
	static int n;
	
    public static void main(String[] args) {
    	
    	arr = new int[]{3, 6, 9};
    	check = new boolean[arr.length];
    	
    	n = 2;  // arr 배열 중에서 뽑을 개수
    	result = new int[2];
    	DFS(0);
    }
    
    static void DFS(int start) {
    	if(start == n) {
    		System.out.println(Arrays.toString(result));
    		return;
    	}
    
    	for(int i=0; i<arr.length; i++) {
    		if(check[i]) continue;
    		
    		check[i] = true;
    		result[start] = arr[i];
    		DFS(start+1);
    		check[i] = false;
    	}
    	
    }
	
}
[3, 6]
[3, 9]
[6, 3]
[6, 9]
[9, 3]
[9, 6]
  • arr 배열인 [3, 6 , 9] 의 원소 3개 중에서 2개를 골라서 순서를 고려하여 출력하기 → ₃P₂
728x90