내 답안
class Solution {
public int solution(int[] arr) {
int answer = 0;
int count = 0;
int[] beforArr = new int[arr.length];
while(count != arr.length) {
count = 0;
beforArr = arr.clone();
for(int i = 0; i < arr.length; i++) {
if(arr[i] % 2 == 0 && arr[i] >= 50) {
arr[i] /= 2;
}else if(arr[i] % 2 != 0 && arr[i] < 50) {
arr[i] = arr[i] * 2 + 1;
}
if(arr[i] == beforArr[i] ) count ++;
}
answer ++;
}
return answer-1;
}
}
Java
복사
다른사람 풀이
import java.util.*;
class Solution {
public int solution(int[] arr) {
int count = 0;
while (true) {
int[] copied = Arrays.copyOf(arr, arr.length);
for (int i = 0 ; i < copied.length ; i++) {
if (copied[i] >= 50 && copied[i] % 2 == 0) {
copied[i] /= 2;
} else if (copied[i] < 50 && copied[i] % 2 == 1) {
copied[i] = copied[i] * 2 + 1;
}
}
if (Arrays.equals(arr, copied)) {
return count;
}
arr = copied;
count++;
}
}
}
Java
복사
class Solution {
public int solution(int[] arr) {
int answer = 0;
while (true){
boolean flag = false;
for (int i = 0; i < arr.length; i++) {
int num = arr[i];
if(num >= 50 && num % 2 == 0){
num /= 2;
flag = true;
} else if(num < 50 && num % 2 != 0){
num = num * 2 + 1;
flag = true;
}
arr[i] = num;
}
answer++;
if(!flag){
break;
}
}
return answer - 1;
}
}
Java
복사