Given a positive integer n and you can do operations as follow:

  1. If n is even, replace n with n/2.
  2. If n is odd, you can replace n with either n + 1 or n - 1.

What is the minimum number of replacements needed for n to become 1?

Example 1:

Input:
8 Output:
3 Explanation:
8 -> 4 -> 2 -> 1

Example 2:

Input:
7 Output:
4 Explanation:
7 -> 8 -> 4 -> 2 -> 1
or
7 -> 6 -> 3 -> 2 -> 1

本题可以直接用递归的方法求解,并可以通过OJ.
 def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 0 if n % 2 == 0:
return self.integerReplacement(n/2) + 1
else:
return min(self.integerReplacement(n-1), self.integerReplacement(n+1)) + 1

不用递归的话,对于偶数的n,处理方式是确定的 n /= 2。关键在于对于奇数的n,处理方式应该是n-1还是n+1。经过观察,如果n + 1是4的倍数(除了3, see L14)。那么应该取加1。比如 n = 7, 15, ...

     def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 0 count = 0
while n > 1:
if n % 2 == 0:
n /= 2
count += 1
elif (n+1) % 4 == 0 and n != 3:
n += 1
count += 1
else:
n -= 1
count += 1 return count
 

Leetcode Integer Replacement的更多相关文章

  1. [LeetCode] Integer Replacement 整数替换

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  2. LeetCode——Integer Replacement

    Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...

  3. LeetCode 397. Integer Replacement

    397. Integer Replacement QuestionEditorial Solution My Submissions   Total Accepted: 5878 Total Subm ...

  4. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  5. Week3 - 397. Integer Replacement

    Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...

  6. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  7. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode "Integer Break"

    A typical CS style DP based solution: class Solution(object): def __init__(self): self.hm = {} def i ...

随机推荐

  1. NOIP2015 Revenge

    辣鸡出题人,毁我比赛,颓我精神,耗我钱财,废我青春. 去年成绩惨不忍睹就不说了...好像是100+80+0+100+50+60. 大概列一下算法. 幻方:模拟 #include <iostrea ...

  2. Nginx/Apache服务连接数梳理

    统计连接数,使用netstat命令或ss命令都可以1)统计连接数(80端口)[root@wang ~]# netstat -nat|grep -i "80"|wc -l872 或者 ...

  3. 实战SQL注入

    SQL注入是啥就不解释了.下面演示一个SQL注入的例子 SQL注入点可以自己尝试或用SQL注入漏洞扫描工具去寻找,这里用大名鼎鼎的sqlmap演示一个现成的案例. 1.漏洞试探 root@kali:~ ...

  4. Web APP开发技巧总结

    http://www.admin10000.com/document/6304.html 1. 当网站添加到主屏幕后再点击进行启动时,可隐藏地址栏(从浏览器跳转或输入链接进入并没有此效果) <m ...

  5. usb驱动开发21之驱动生命线

    现在开始就沿着usb_generic_driver的生命线继续往下走.设备的生命线你可以为是从你的usb设备连接到hub的某个端口时开始,而驱动的生命线就必须得回溯到usb子系统的初始化函数usb_i ...

  6. 利用Spring MVC搭建REST Service

    之前写过一篇 利用JAX-RS快速开发RESTful 服务 今天来看下spring-mvc框架如何实现类似的功能: 一.pom.xml <?xml version="1.0" ...

  7. 乐易贵宾VIP教程:百度贴吧 - QQ部落 - QQ空间 Post实战系列视频课程

    教程挺不错,3套案例的实战,有需要的可以看一下百度贴吧课程目录:1.百度登录抓包分析2.百度登录[代码实现]3.百度验证码登录[代码实现]4.贴吧关注[抓包分析]5.贴吧关注(代码编写)6.贴吧签到[ ...

  8. Beta版本项目展示要求

    项目评审的定在1月5日上午9:00在新主楼D225进行. 在Beta阶段项目评审会上, 每个团队有12分钟展示时间,10分钟问答和机动时间,我们的展示也不需要PPT,大家把要展现的东西写成博客(可以有 ...

  9. 写个PHP框架吧

    肯定会问:现在的PHP框架那么多了,为什么还要写一个PHP框架呢? 1.时代:PHP7来了,现在的所有框架都是基于PHP5.x的.到时候PHP7正式推广出来,现有的框架都不能发挥PHP7的最大性能优势 ...

  10. C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别

    C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...