https://leetcode.com/problems/permutations/
Permutations - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
import java.util.ArrayList;
import java.util.List;
public class Permutations {
    public static void main(String[] args) {
        System.out.println(permute(new int[] {1}));
    }
    static List<List<Integer>> answer = new ArrayList<>();
    public static List<List<Integer>> permute(int[] nums) {
        for(int i = 0 ; i < nums.length; i++) {
            boolean[] boolList = new boolean[nums.length];
            boolList[i] = true;
            List<Integer> paramList = new ArrayList<>();
            paramList.add(nums[i]);
            dfs(paramList, nums,boolList);
        }
        return answer;
    }
    public static void dfs(List<Integer> paramList, int[] nums, boolean[] boolList) {
        if(paramList.size() == nums.length) {
            List<Integer> answerList = new ArrayList<>(paramList);
            answer.add(answerList);
        }else if(paramList.size() < nums.length){
            for(int i = 0; i < nums.length; i++) {
                if(!boolList[i]) {
                    paramList.add(nums[i]);
                    boolList[i] = true;
                    dfs(paramList, nums,boolList);
                    paramList.remove(Integer.valueOf(nums[i]));
                    boolList[i] = false;
                }
            }
        }
    }
}
'Programming > Algorithm' 카테고리의 다른 글
| [leetcode] RomanToInt (0) | 2020.11.29 | 
|---|---|
| [leetcode] PopulatingNextRightPointersinEachNode (0) | 2020.11.29 | 
| [leetcode] PacificAtlanticWaterFlow (0) | 2020.11.29 | 
| [leetcode] NumberofIslands (0) | 2020.11.29 | 
| [leetcode] String to Integer (atoi) (0) | 2020.11.29 |