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 n is odd, you can replace n with either
n + 1
orn - 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
本题可以直接用递归的方法求解,并可以通过OJ.
def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 0 if n % 2 == 0:
return self.integerReplacement(n/2) + 1
else:
return min(self.integerReplacement(n-1), self.integerReplacement(n+1)) + 1
不用递归的话,对于偶数的n,处理方式是确定的 n /= 2。关键在于对于奇数的n,处理方式应该是n-1还是n+1。经过观察,如果n + 1是4的倍数(除了3, see L14)。那么应该取加1。比如 n = 7, 15, ...
def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 0 count = 0
while n > 1:
if n % 2 == 0:
n /= 2
count += 1
elif (n+1) % 4 == 0 and n != 3:
n += 1
count += 1
else:
n -= 1
count += 1 return count
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
Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...
- 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 ...
随机推荐
- centos安装docker
一.升级内核 [root@iZ2893wjzgyZ ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org [root@iZ289 ...
- 采用dlopen、dlsym、dlclose加载动态链接库【总结】(转)
1.前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将各个业务已动态链接库的形式加载进来,这就是所谓的插件.linux提供了加载和处理动态链接库的系统 ...
- QT 常用控件二
QT提供QHBoxLayout类.QVBoxlayout类及QGridLayout类等的基本布局管理,分别是水平排列布局,垂直排列布局和网格排列布局 addWidget()方法用于向布局中加入需要布局 ...
- javascript:查找“跳号”号码
业务背景:航空货运系统中,“货运代理商”会定期从“航空公司”领取一定数量的纸质运单(每张纸上有一个单号),这些单号都是连续的(即:每次可以理解为领取一个“号段”),而且每张单子都要向航空公司交纳一定的 ...
- Theano2.1.5-基础知识之打印出theano的图
来自:http://deeplearning.net/software/theano/tutorial/printing_drawing.html Printing/Drawing Theano gr ...
- opencv6.4-imgproc图像处理模块之直方图与模板
接opencv6.3-imgproc图像处理模块之边缘检测 九.直方图的相关操作 直方图是图像中像素强度分布的图形表达方式:它统计了每一个强度值所具有的像素个数 上图是一个灰色图像,通过对图像的每个不 ...
- Node 进阶:express 默认日志组件 morgan 从入门使用到源码剖析
本文摘录自个人总结<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 章节概览 morgan是express默认的日志中间件, ...
- 一例完整的websocket实现群聊demo
前言 业余我都会花一些时间在tcp.http和websocket等领域的学习,现在觉得有点收获,所以把一个基于websocket的群聊功能的例子提供给大家玩玩.当然这是一个很完整的例子,包括webso ...
- linux开机自动启动
1 .vi /etc/rc.local 2.编写开机后运行的命令 如:service httpd start
- Set Php show errors
php中的Error等级分成16类,用一个16位的数值表示这16种集合元素.下面是从php.ini中截取的: ; Error Level Constants: ; E_ALL - All errors ...