배열만들기 2

gotcha!!·2023년 8월 1일
0

코딩테스트

목록 보기
16/36

문제

내 코드

import java.util.*;
class Solution {
    public int[] solution(int l, int r) {
        
        ArrayList<Integer>list = new ArrayList<Integer>();
        for(int i = l; i<=r; i++){
            if(containZF(i)){ // 밑에 만들어놓은 메서드로 5,0이 포함되는 숫자 리스트에 추가
                list.add(i);
            }
        }
        if(list.isEmpty()){ // 배열이 빈지 체크
            list.add(-1);
        }
        
        int[] answer = new int[list.size()];
        for(int h = 0; h<list.size(); h++){
            answer[h] = list.get(h);
        }
        
        return answer;
    }
    
     public static boolean containZF(int num) {
        while (num > 0) { // 0,5 포함하는지 체크
            int d = num % 10;
            if (d != 0 && d != 5) {
                return false;
            }
            num /= 10;
        }
        return true;
    }
}
profile
ha lee :)

0개의 댓글