5일차 - 이어 붙인 수

wsung·2025년 11월 15일

5. 이어 붙인 수

문제 설명
정수가 담긴 리스트 num_list가 주어집니다. num_list의 홀수만 순서대로 이어 붙인 수와 짝수만 순서대로 이어 붙인 수의 합을 return하도록 solution 함수를 완성해주세요.

class Solution {
    public int solution(int[] num_list) {
        String odd = "";
        String even = "";
        
        for(int i = 0; i<num_list.length; i++) {
            if( num_list[i] % 2 == 0) {
                odd += num_list[i];
            } else {
                even += num_list[i];
            }
        }
        int odd2 = Integer.parseInt(odd);
        int even2 = Integer.parseInt(even);
        
        int answer = odd2 + even2;
        
        return answer;
    }
}
  • String 에 정수를 그대로 더하고싶다 : "" 빈문자열 사용

  • String형 정수를 Int형으로 바꾸고싶다 : Integer.paseInt(string) 사용

[링크: 이어 붙인 수]

profile
0부터 시작하는 백엔드

0개의 댓글