코딩테스트 풀이/JAVA
[프로그래머스/Lv.0] 저주의 숫자 3
무지맘
2023. 5. 30. 13:07
1. Input, Output, Example
3x 마을 사람들은 3을 저주의 숫자라고 생각하기 때문에 3의 배수와 숫자 3을 사용하지 않는다.
- n을 3x 마을에서 사용하는 숫자로 바꿔 반환
2. Constraint
1) 1 ≤ n ≤ 100
3. Code
1) 첫 코드(2023/05/30)
import java.util.*;
class Solution {
public int solution(int n) {
int i = 1;
List<Integer> list = new ArrayList<>();
while(list.size()<n){
boolean check = false;
if(i%3==0)
check = true;
int num = i;
while(num>1 && !check){
if(num%10==3)
check = true;
num /= 10;
}
if(!check) list.add(i);
i++;
}
return list.get(n-1);
}
}
2) 다른 사람의 풀이 중 마음에 들었던 것(2023/05/30)
class Solution {
public int solution(int n) {
String str;
for (int i = 1; i <= n; i++){
str = String.valueOf(i);
if(str.contains("3") || i%3 == 0) n++;
}
return n;
}
}