문제
내가 작성한 정답
class Solution {
public int solution(int order) {
int answer = 0;
String orders = String.valueOf(order);
for(char c: orders.toCharArray()){
if(c=='3'||c=='6'||c=='9'){
answer++;
}
}
return answer;
}
}
다른 사람들의 정답
class Solution {
public int solution(int order) {
int answer = 0;
// order를 문자열로 변환
String str = order+"";
for(int i=0; i<str.length(); i++){
char c = str.charAt(i);
if(c=='3'||c=='6'||c=='9') answer++;
}
return answer;
}
}
order + ""는 order를 문자열로 변환하는 간단한 방법
class Solution {
public int solution(int order) {
int answer = 0;
int count = 0;
while(order != 0)
{
if(order % 10 == 3 || order % 10 == 6 || order % 10 == 9)
{
count++;
}
// 모든 자리수를 검사하기 위해 10으로 나눠서 위의 과정 반복
order = order/10;
}
answer = count;
return answer;
}
}
Share article