본문 바로가기

알고리즘 테스트 공부

콜라츠 수열만들기

 

import java.util.*;

class Solution {
    public int[] solution(int n) {
        List<Integer> list = new ArrayList<>();
        list.add(n);
        while(n != 1){
            
            if(n % 2 == 0){
            n = n / 2 ;
            list.add(n);
                continue;
            
        }else if(n == 1){
           list.add(1);
                break;
        }else{
             n = n * 3 + 1 ;
            list.add(n);
                 continue;
        }
        
        }
        
        //int[] answer = list.stream().mapToInt(i->i).toArray();
        int[] answer = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            answer[i] = list.get(i);
        }
        return answer;
    }
}

 

 

 

다른풀이!!

import java.util.ArrayList;
import java.util.List;

class Solution {
    public int[] solution(int n) {
        List<Integer> sequence = new ArrayList<>();
        sequence.add(n);

        while (n != 1) {
            if (n % 2 == 0) {
                n /= 2;
            } else {
                n = 3 * n + 1;
            }
            sequence.add(n);
        }

        int[] answer = new int[sequence.size()];
        for (int i = 0; i < sequence.size(); i++) {
            answer[i] = sequence.get(i);
        }
        return answer;
    }
}

n이 짝수인 경우, n을 2로 나눕니다.
n이 홀수인 경우, n에 3을 곱하고 1을 더합니다.
각 작업을 수행한 결과인 n을 sequence에 추가합니다.
반복문이 종료된 후, sequence의 내용을 배열로 변환하여 answer 배열에 저장하고 반환합니다.

 

 

이게더 깔꼼쓰,,,클린하다,,,!! 좀더 고민해보자!!

 

import java.util.stream.IntStream;

class Solution {
    public int[] solution(int n) {
        return IntStream.concat(
                        IntStream.iterate(n, i -> i > 1, i -> i % 2 == 0 ? i / 2 : i * 3 + 1),
                        IntStream.of(1))
                .toArray();
    }
}
import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int[] solution(int n) {
        Queue<Integer> answer = new LinkedList<>();
        while (n > 1) {
            answer.add(n);
            if (n % 2 == 0) n >>= 1;
            else n = n * 3 + 1;
        }
        answer.add(1);
        return answer.stream().mapToInt(i -> i).toArray();
    }
}
import java.util.*;
import java.util.stream.Stream;

class Solution {
    public int[] solution(int n) {
        return Stream.iterate(n, i -> i >= 1, i -> i == 1 ? 0 : i % 2 == 0 ? i / 2 : 3 * i + 1).mapToInt(Integer::intValue).toArray();
    }
}
class Solution {
    public int[] solution(int n) {
        String str = n+",";
        while(n!=1){
            n = n%2==0 ? n/2 : 3*n+1;
            str += n+",";
        }
        String[] arr = str.split(",");
        int[] answer = new int[arr.length];
        for(int i=0; i<answer.length; i++){
            answer[i] = Integer.parseInt(arr[i]);
        }
        return answer;
    }
}
import java.util.*;

class Solution {
    public List<Integer> solution(int n) {
        List<Integer> answer = new ArrayList<>();
        while(n>1) {
            answer.add(n);
            if(n%2==0) {
                n=n/2;
            } else {
                n=3*n+1;
            }
        }
        answer.add(1);
        return answer;
    }
}

'알고리즘 테스트 공부' 카테고리의 다른 글

배열만들기 4  (1) 2024.01.08
간단한 논리 연산  (0) 2024.01.05
카운트 업  (1) 2024.01.03
배열 만들기 2  (0) 2024.01.02
수열과 구간 쿼리 4  (0) 2024.01.01