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. Jenkins 持续集成配置

    Jenkins搭建.NET自动编译测试与发布环境 Jenkins之Deploy部署(包括站点和类库项目) * 续篇--TFS+MSbuild+jenkins 实现 持续集成+自动部署到WEB网站 Je ...

  2. AJAX Form Submit Framework 原生js post json

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest <!doctype ht ...

  3. mysql备份总结

    w汇总对比. mysqldump -u user -p wdbname > /www/wbak.sql pwd CREATE TABLE wbak_w_02071349 LIKE w; INSE ...

  4. Girls' research---hdu3294(回文子串manacher)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 给出一个字符串和加密的字符规律 例如 c abcba c代表把串中的c改成a,d改成b... b ...

  5. 持续集成之戏说Check-in Dance(转)

    add by zhj: 先说一下持续集成的定义,这是ThoughtWorks首席科学家Martin Fowler在<持续集成>第二版中给出的,“持续集成是一种软件开发实践.在持续集成中,团 ...

  6. PAE 分页模式详解

    2016-11-18 记得之前看windows内核原理与实现的时候,在内存管理部分,看到涉及到PAE模式的部分,提到此模式下可以让系统在虚拟地址还是32位宽的情况下,支持64GB的物理内存或者更多.当 ...

  7. Python 中的匿名函数,你滥用了吗?

    概念 我们从一个例子引入. 这里有一个元素为非空字符串的列表,按字符串最后一个字母将列表进行排序.如果原列表是 ['abc', 'g', 'def'],则结果应该是 ['abc', 'def', 'g ...

  8. Unity3D优化技巧系列七

    笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑.畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  9. SpringBoot-模板渲染

    模板 开发Web站点的本质,其实就是根据浏览器发起的请求(输入),生成HTML代码返回给浏览器(输出).在之前的学习中,我们已经通过文件的形式存储起来而不是直接在Java代码中生成HTML代码.另一方 ...

  10. Does Daemon Thread Exit with Main Thread?

    主线程(进程)退出后,主线程创建的守护线程也会退出吗? 通过下面的代码测试: Demo1: 进程创建普通线程 #!/usr/bin/python3 # FileName: daemonThread.p ...