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. activate mod_rewrite How To Set Up mod_rewrite for Apache on Ubuntu 14.04 Apache Rewrite url重定向功能的简单配置

    https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-0 ...

  2. GraphicsMagick 1.3.25 Linux安装部署

    1.安装相关依赖包 yum install -y gcc libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-d ...

  3. golang zlib 压缩,解压缩

    package main import ( "bytes" "compress/zlib" "fmt" "io" &qu ...

  4. redis之数据操作详解

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  5. oracle入门(7)——存储过程

    [本文介绍] 熟悉了PL/SQL语法后,实现java调用oracle存储过程才是主要目的.本文将介绍如何写存储过程,java如何调用存储过程. [存储过程介绍] 抛开专业的描述,存储过程就是在数据库里 ...

  6. Oracle 性能调优 10053事件

    思维导图 10053事件概述 我们在查看一条SQL语句的执行计划时,只看到了CBO最终告诉我们的执行计划结果,但是我们并不知道CBO为何要这样做. 特别是当执行计划明显失真时,我们特别想搞清楚为什么C ...

  7. mysql lock

    http://blog.chinaunix.net/uid-21505614-id-289450.html http://bbs.csdn.net/topics/340127237 http://ww ...

  8. 我与前端之间不得不说的三天两夜之html基础

    HTML 初识 分类 cs模式 client-server bs模式 Browser-server web服务本质 from socket import * def main(): service=s ...

  9. mysql update 多表 (复制)

    定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Price:另外一张表是ProductPrice表,我们要将ProductPrice表中的价格字段Price更新为Price表中价 ...

  10. Mysql 压力测试工具 mysqlslap

    转载至文章作者:杜亦舒 链接:https://www.sdk.cn/news/4512 来源:SDK.cn 摘要:mysqlslap 是 Mysql 自带的压力测试工具,可以模拟出大量客户端同时操作数 ...