给定一个正整数 n,你可以做如下操作:
1. 如果 n 是偶数,则用 n / 2
替换 n。
2. 如果 n 是奇数,则可以用 n + 1
或n - 1
替换 n。
n 变为 1 所需的最小替换次数是多少?
示例 1:
输入:8 输出:3 解释:8 -> 4 -> 2 -> 1
示例 2:
输入:7 输出:4 解释:7 -> 8 -> 4 -> 2 -> 1或7 -> 6 -> 3 -> 2 -> 1
class Solution {
public:
int integerReplacement(int n) {
if (n == 1) return 0;
if (n % 2 == 0) return 1 + integerReplacement(n / 2);
else {
long long t = n;
return 2 + min(integerReplacement((t + 1) / 2), integerReplacement((t - 1) / 2));
}
}
};