Question

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

If n is even, replace n with n/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

Solution

这题比较有技巧。递归。偶数的时候直接除2,奇数的时候分两种情况,一种是(i+1) % 4 == 0的,表示(i+1)/2以后还是偶数,那么就可以一直除2除到为1为止,否则选择i - 1会更快。

Code

class Solution {
public:
int integerReplacement(int n) {
if (n == 1)
return 0;
if (n == 2)
return 1;
if (n == 3)
return 2;
if (n == INT_MAX)
return 32;
if (!(n & 1))
return integerReplacement(n / 2) + 1;
else {
if ((n + 1) % 4 == 0)
return integerReplacement(n + 1) + 1;
else
return integerReplacement(n - 1) + 1;
}
}
};

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

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

  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. [LeetCode] 9.Palindrome Number - Swift

    Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...

  2. 2018 又一个假期 over 了

    五一假期三天  在家窝三天  早上起床仍感觉没休息好  其实确实没休息好  假期的时间总不愿意拿来补觉用.周末准备去体检,真心恐惧医院的味道,硬着头皮还得上啊.

  3. python学习笔记(十九)发送邮件

    在python开发项目或者做自动化测试时候,在测试完成后需要将测试结果总结后进行上报,那么我们就可以通过发送邮件来完成这项工作. 下面我们来看看python中怎么发送邮件的,python中发送邮件可以 ...

  4. Oracle 常见问题汇总

    1.Listener refused the connection with the following error 安装之后如果遇到如下问题状态: 失败 -测试失败: Listener refuse ...

  5. Objective-C中的alloc和init问题

    从开始学的NSString *name=[[NSString alloc] init] 起,仅仅这句话是分配内存空间,一直在用,从来没考虑过它的内部是怎么实现的.今天无意中看到了这一句代码: NSSt ...

  6. C#windows应用程序打包(VS2010+SQLServer2008)

    C#windows应用程序打包(VS2010+SQLServer2008) 开发环境:VS2010+SQL Server 2008 操作系统:win7_32bit 旗舰版 开发语言:C# 项目名称:学 ...

  7. [C语言]删除用户自定义后缀名的所有文件

    环境:win7 IDE:DEV-C++ 编译器:GCC 编译结果:Success 运行结果:Success 使用说明: 1.输入需要查询的目录,比如e: 2.输入需要删除的后缀名:比如:txt 注意: ...

  8. 【android】activity、fragment传值例子

    1:Activity篇 1.1向Activity传值 关键点在于putExtra.如果传递类的话,记得类实现Serializable接口 Intent intent = new Intent(Firs ...

  9. 理解js的DOM操作

    1.DOM结构——两个节点之间可能存在哪些关系以及如何在节点之间任意移动.document.documentElement     返回文档的根节点<html> document.body ...

  10. 使用Xib创建自定义视图(不是cell)时需要注意的问题

    开发项目过程中,有些地方不免会用到Xib来提高开发效率,如果你的手速够快,写代码建视图,我并不反对这样做.因为我以前也是纯手写代码开发. 进入正题,Xib好用,但是这些下面这些问题需要注意一下. 问题 ...