반응형
저주의 숫자 3
class Solution {
public int solution(int n) {
int tenc = 1;
int thc = 1;
while(tenc <= n){
if(thc % 3 == 0){
thc++;
continue;
}
else if(String.valueOf(thc).contains("3")){
thc++;
continue;
}
thc++;
tenc++;
}
return thc - 1;
}
}
평행
class Solution {
public int solution(int[][] dots) {
return isParallel(dots[0], dots[1], dots[2], dots[3]) ||
isParallel(dots[0], dots[2], dots[1], dots[3]) ||
isParallel(dots[0], dots[3], dots[1], dots[2]) ? 1 : 0;
}
private boolean isParallel(int[] a, int[] b, int[] c, int[] d) {
return (b[1] - a[1]) * (d[0] - c[0]) == (d[1] - c[1]) * (b[0] - a[0]);
}
}
겹치는 선분의 길이
import java.util.Arrays;
import java.util.Comparator;
class Solution {
public static int solution(int[][] lines) {
int answer = 0;
Arrays.sort(lines, Comparator.comparingInt(o -> o[0]));
int start_idx = lines[0][0];
int end_idx = lines[0][1];
for(int i = 1; i < lines.length; i++) {
int curStart = Math.max(start_idx, lines[i][0]);
int curEnd = Math.min(end_idx, lines[i][1]);
if(curStart < curEnd){
answer += curEnd - curStart;
start_idx = curEnd;
end_idx = Math.max(end_idx,lines[i][1]);
continue;
}
start_idx = lines[i][0];
end_idx = lines[i][1];
}
return answer;
}
}
유한소수 판별하기
class Solution {
public int solution(int a, int b) {
int gcd = getGCD(a, b);
b /= gcd;
while (b % 2 == 0) b /= 2;
while (b % 5 == 0) b /= 5;
return b == 1 ? 1 : 2;
}
private int getGCD(int a, int b) {
if (b == 0) return a;
return getGCD(b, a % b);
}
}
728x90
반응형
'CodingTest' 카테고리의 다른 글
코딩테스트 입문 - Day 24 (0) | 2025.05.11 |
---|---|
코딩테스트 입문 - Day 23 (0) | 2025.05.11 |
유클리드 호제법 - 최대공약수, 유한소수 판별하기 (0) | 2025.05.06 |
코딩테스트 입문 - Day 21 (0) | 2025.05.05 |
코딩테스트 입문 - Day 20 (1) | 2025.05.05 |