
내가 작성한 코드
class Solution {
public int solution(int n, int a, int b) {
int round = 1;
// a와 b가 같은 라운드에서 만날 때까지 반복
while (true) {
// a와 b가 바로 붙어있는 번호이면서, 둘 중 하나가 짝수이고, 둘의 차이가 1이면 만나는 경우
if ((a % 2 == 0 && a - 1 == b) || (b % 2 == 0 && b - 1 == a)) {
return round;
}
// 다음 라운드 번호로 갱신 >> 두 수 중 하나를 홀수로 두기 위해 +1
a = (a + 1) / 2;
b = (b + 1) / 2;
round++;
}
}
}
다른 사람의 코드
class Solution {
public int solution(int n, int a, int b) {
//(a-1) XOR (b-1)값을 이진수 문자열로 변환한 길이 세기
return Integer.toBinaryString((a - 1) ^ (b - 1)).length();
}
}
Share article