My code:
public class Solution {
public int integerReplacement(int n) {
if (n == Integer.MAX_VALUE) {
return 32;
}
int cnt = 0;
while (n != 1) {
if (n % 2 == 0) {
n /= 2;
}
else if ((n + 1) % 4 == 0 && n != 3) {
n++;
}
else {
n--;
}
cnt++;
}
return cnt;
}
}
这个解释讲的挺不错的
Anyway, Good luck, Richardo! — 10/14/2016