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. [iOS微博项目 - 4.2] - 设置转发微博背景

    github: https://github.com/hellovoidworld/HVWWeibo A.转发微博部分的淡灰色背景 1.需求 转发微博部分需要设置背景色 使用图片作为背景   2.思路 ...

  2. Mac自带Apache和Php

    Mac 是默认安装 apache和php,但是需要使用root用户来启用,所以请按照我下面的步骤来: 一.启用root用户1.选取系统偏好设置....2.从显示菜单中,选取“帐户”.3.点按锁图标并使 ...

  3. 剑指Offer——求1+2+3+...+n

    题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 分析: 递归实现. 代码: class So ...

  4. etcd: request cluster ID mismatch错误解决【只适用于新建etcd集群或无数据集群】

    1.报错信息 Mar 29 05:45:31 xxx etcd: request cluster ID mismatch (got 414f8613693e2e2 want cdf818194e3a8 ...

  5. javascript 之 typeof 与 instanceof

    1.typeof:返回一个表达式的数据类型的字符串 返回结果为js的数据类型,包括number,boolean,string,object,undefined,function. var a = 1; ...

  6. 3类数据库的联动:mysql、mongodb、redis

    3类数据库的联动:mysql.mongodb.redis from pymysql import * from pymongo import * from redis import * class M ...

  7. 超简单Centos+Docker+Halo搭建java向博客

    首先,我就当你们了解docker基本知识了. 直接开始,全新的系统. 1. 安装Docker 移除旧的版本:  $ sudo yum remove docker \                  ...

  8. 003-SpringBoot导入xml配置

    SpringBoot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Configuration类开始,你可以使用@ImportResouce注解加载XML配置文件,我拿一个 ...

  9. 怎么解决tomcat占用8080端口问题图文教程

     怎么解决tomcat占用8080端口问题 相信很多朋友都遇到过这样的问题吧,tomcat死机了,重启eclipse之后,发现 Several ports (8080, 8009) required ...

  10. POJ3176:Cow Bowling(数字三角形问题)

    地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...