내 답안
import java.util.ArrayList;
import java.util.Arrays;
class Solution {
public int[] solution(int l, int r) {
ArrayList<Integer> resultList = new ArrayList<>();
for (int i = l; i <= r; i++) {
if (String.valueOf(i).matches("[05]+")) {
resultList.add(i);
}
}
if (resultList.isEmpty()) {
return new int[]{-1};
}
int[] resultArray = new int[resultList.size()];
for (int i = 0; i < resultList.size(); i++) {
resultArray[i] = resultList.get(i);
}
return resultArray;
}
}
Java
복사
다른 사람 풀이법
import java.util.ArrayList;
class Solution {
public int[] solution(int l, int r) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i < 64; i++) {
int num = Integer.parseInt(Integer.toBinaryString(i)) * 5;
if (l <= num && num <= r)
list.add(num);
}
return list.isEmpty() ? new int[] { -1 } : list.stream().mapToInt(i -> i).toArray();
}
}
Java
복사
import java.util.*;
class Solution {
public List<Integer> solution(int l, int r) {
List<Integer> list = new ArrayList<>();
loop :
for (int i = l; i <= r; i++) {
int j = i;
while(j != 0) {
if (j % 5 != 0) {
continue loop;
}
j /= 10;
}
list.add(i);
}
if (list.isEmpty()) {
list.add(-1);
}
return list;
}
}
Java
복사
import java.util.stream.IntStream;
class Solution {
public int[] solution(int l, int r) {
int[] answer = IntStream.rangeClosed(l, r).filter(i -> {
while (i > 0) {
if (i % 5 != 0) return false;
i /= 10;
}
return true;
}).toArray();
return answer.length == 0 ? new int[]{-1} : answer;
}
}
Java
복사