본문 바로가기
Algorithm

[Algorithm][Recursion] 문제 풀이 #18 - Combinations

by Lee David 2022. 8. 4.
반응형
문제 링크

https://leetcode.com/problems/combinations/

내 코드
List<List<Integer>> result;
LinkedList<Integer> current;

public List<List<Integer>> combine(int n, int k) {
    result = new ArrayList<>();
    current = new LinkedList();
    recursion(1, n, k);
    return result;
}

private void recursion(int start, int n, int k) {
    if(current.size()==k){
        result.add(new ArrayList(current));
        return;
    }

    for(int i=start;i<=n;i++){
        current.add(i);
        recursion(i+1, n, k);
        current.removeLast();
    }
}
결과

잘 몰라도 고민하고 답을 보니 내가 푸는 방향이 틀리지 않았다는게 보인다.

필요 이상으로 시간을 쏟을 필요 없는것을 깨닫는 것 같다.

배운다는 생각으로 하나씩 코드를 뜯다보니 재밋다. 

처음에 의무감으로 시작한 일이 조금씩 재밋어지는것 같다.

반응형