Python | Leetcode Python题解之第397题整数替换
题目:
题解:
class Solution:def integerReplacement(self, n: int) -> int:ans = 0while n != 1:if n % 2 == 0:ans += 1n //= 2elif n % 4 == 1:ans += 2n //= 2else:if n == 3:ans += 2n = 1else:ans += 2n = n // 2 + 1return ans