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. linux下安装安装pcre-8.32 configure: error: You need a C++ compiler for C++ support

    linux下安装安装pcre-8.32./configure --prefix=/usr/local/pcre 出现以下错误configure: error: You need a C++ compi ...

  2. Codevs1026 逃跑的拉尔夫

    题目描述 Description 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且车上装有用于发射车子移动路线的装置. 那个装置太旧了,以至于只能发射关于那辆车的移动 ...

  3. 【转】【MySql】mysql存储过程中的异常处理

    定义异常捕获类型及处理方法: DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement ...

  4. Firefox使用svg blur滤镜渲染图片

    很久没来更新博客了,今天正好比较闲,就写一篇手头项目上遇到的一个css问题: .mature .blur { -webkit-filter:blur(25px); -moz-filter:blur(2 ...

  5. salt基本原理

            转载自: 来自:http://tech.mainwise.cn/?p=438     说明:salt是一个异构平台基础设置管理工具(虽然我们通常只用在Linux上),使用轻量级的通讯器 ...

  6. Android 开发之旅:view的几种布局方式及实践

    本文的主要内容就是分别介绍以上视图的七种布局显示方式效果及实现,大纲如下: 1.View布局概述 2.线性布局(Linear Layout) 2.1.Tips:android:layout_weigh ...

  7. 阿里云安装LNMP以及更改网站文件和MySQL数据目录

    LNMP安装了哪些软件?安装目录在哪LNMP相关软件安装目录Nginx 目录: /usr/local/nginx/MySQL 目录 : /usr/local/mysql/MySQL数据库所在目录:/u ...

  8. 前端见微知著工具篇:Bower组件管控

    在上一篇文章中,我们提到了利用Grunt可以完成的内容,其中最主要的功能就是利用各种Node的组件来搭配出一个自动化高亮,自动化运行等的Web前端开发环境.但是Bower也是一个专门来管理各种依赖组件 ...

  9. 使用EasyUI布局时出现混乱瞬间的解决方法

    在所有form代码之前加遮罩层 <div id='PageLoadingTip' style="position: absolute; z-index: 1000; top: 0px; ...

  10. Spring 依赖注入方式详解

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...