본문 바로가기

알고리즘 테스트 공부

카운트 업

import java.util.*;
class Solution {
    public List solution(int start_num, int end_num) {
        List<Integer> answer = new ArrayList<>();
        for(int i=start_num; i<=end_num; i++){
            answer.add(i);
        }
        
        return answer;
    }
}

 

이제야 조금씩 배열을 알게되는거같다!

처음에 리턴을 int[]이상태로 해서 오류났지만 리턴타입을 List 변경해줌으로 해결봄!! 정신차리고 하나씩 쭉쭉 풀자!!

 

 

 

다른풀이!!!

import java.util.stream.IntStream;

class Solution {
    public int[] solution(int start, int end) {
        return IntStream.rangeClosed(start, end).toArray();
    }
}
class Solution {
    public int[] solution(int start, int end) {
        int[] answer = new int[end - start+1];
        for(int i =0; i<= end - start; i++) {
            answer[i] = start + i;
        }
        return answer;
    }
}
class Solution {
    public int[] solution(int start, int end) {
        int[] answer = new int[end-(start-1)];
        for(int i=0; i<answer.length; i++){
            answer[i] = start;
            start++;
        }
        return answer;
    }
}

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

간단한 논리 연산  (0) 2024.01.05
콜라츠 수열만들기  (0) 2024.01.04
배열 만들기 2  (0) 2024.01.02
수열과 구간 쿼리 4  (0) 2024.01.01
수열과 구간 쿼리 2  (0) 2023.12.29