콜라츠 수열

gotcha!!·2023년 8월 2일
0

코딩테스트

목록 보기
18/36

문제

내 코드

import java.util.*;
class Solution {
    public int[] solution(int n) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        int num = 0;
        list.add(n);
        while(n != 1){
            num = (n % 2 == 0) ? n / 2 : 3 * n + 1;
            list.add(num);
            n = num;
        }
        int answer[] = new int[list.size()];
        for(int i = 0; i < answer.length; i++){
            answer[i] = list.get(i);
        }
        return answer;
    }
}
profile
ha lee :)

0개의 댓글