문제
내가 작성한 정답
class Solution {
public int solution(int[] array) {
int answer = 0;
for(int a : array){
String brray = Integer.toString(a);
for(int b : brray.toCharArray()) {
if(b==(char)7+48){
answer++;
}
}
}
return answer;
}
}
다른 사람들의 정답
class Solution {
public int solution(int[] array) {
int answer = 0;
for(int a : array){
while(a != 0){
// 10으로 나눠 한자리씩 검사
if(a % 10 == 7){
answer++;
}
a /= 10;
}
}
return answer;
}
}
StreamAPI
import java.util.*;
import java.util.stream.Collectors;
class Solution {
public int solution(int[] array) {
return (int) Arrays.stream(
Arrays.stream(array) // 1단계: 배열을 스트림으로 변환
.mapToObj(String::valueOf) // 2단계: 각 정수를 문자열로 변환
.collect(Collectors.joining()) // 3단계: 모든 문자열을 하나로 합치기
.split("") // 4단계: 문자열을 문자 배열로 분리
)
.filter(s -> s.equals("7")) // 5단계: "7"인 문자만 필터링
.count(); // 6단계: 필터링된 요소의 개수 세기
}
}

Share article