题目:
题解:
class Solution {
public int integerReplacement(int n) {
int ans = 0;
while (n != 1) {
if (n % 2 == 0) {
++ans;
n /= 2;
} else if (n % 4 == 1) {
ans += 2;
n /= 2;
} else {
if (n == 3) {
ans += 2;
n = 1;
} else {
ans += 2;
n = n / 2 + 1;
}
}
}
return ans;
}
}