LeetCode——Integer Replacement
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的更多相关文章
- [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 ...
- 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 ...
- LeetCode 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- Week3 - 397. Integer Replacement
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode "Integer Break"
A typical CS style DP based solution: class Solution(object): def __init__(self): self.hm = {} def i ...
随机推荐
- However, a general purpose protocol or its implementation sometimes does not scale very well.
https://netty.io/wiki/user-guide-for-4.x.html The Problem Nowadays we use general purpose applicatio ...
- Apache Kafka源码分析 – Log Management
LogManager LogManager会管理broker上所有的logs(在一个log目录下),一个topic的一个partition对应于一个log(一个log子目录)首先loadLogs会加载 ...
- TCP和UDP的区别?
答:TCP提供面向连接的.可靠的数据流传输,而UDP提供的是非面向连接的.不可靠的数据流传输.TCP传输单位称为TCP报文段,UDP传输单位称为用户数据报.TCP注重数据安全性,UDP数据传输快,因为 ...
- mac配置python自然语言处理环境
一.nltk安装 Ⅰ.工具安装步骤 1.根据python版本从 https://pypi.python.org/pypi/setuptools 下载对应版本的setuptools.然后,在终端下运行, ...
- Android集成百度地图SDK
本Demo中所含功能 1:定位,显示当前位置 2:地图多覆盖物(地图描点.弹出该点的具体信息) 3:坐标地址互相换算 4:POI兴趣点检索 5:线路查询(步行,驾车,公交) 6:绘制线路(OpenGL ...
- Data striping
条带化是把连续的数据分割成相同大小的数据块,把每段数据分别写入到阵列中的不同磁盘上的方法. 当多个进程同时访问一个磁盘时,可能会出现磁盘冲突.大多数磁盘系统都对访问次数(每秒的 I/O 操作,IOPS ...
- WebHDFS vs HttpFS GateWay
基于hadoop 2.7.1版本 一.简介 1. WebHDFS官方简介: Introduction The HTTP REST API supports the complete FileSyste ...
- php使用amqplib方式使用rabbitmq
安装 sudo apt-get install php sudo apt-get install rabbitmq-server sudo apt-get install php-bcmath php ...
- JVM之参数分配详解
开篇之前,推荐一个关于JVM很不错的博客:http://www.cnblogs.com/redcreen/archive/2011/05/04/2036387.html 一.堆参数设置 -XX:+Pr ...
- NOSQL概念入门
一.NOSQL概念 随着大数据时代的到来,分布式存储得到了快速发展,其中比较受欢迎的,主要以key-value键值对存储的非关系型数据库进入了大家的视野. NOSQL的全称是Not Only Sql, ...