class Solution:
def integerReplacement(self, n: int) -> int:
res=0
while n!=1:
if n%2==0:
n=int(n/2)
res=res+1
elif n==3:
n=1
res=res+2
else:
if bin(n-1).count('1') < bin(n+1).count('1'):
n=n-1
else:
n=n+1
res=res+1
return res
LeetCode397.整数替换(Python3)
最新推荐文章于 2022-08-23 23:28:20 发布