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 + 1orn - 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 ...
随机推荐
- rpc使用举例
#server.py from SimpleXMLRPCServer import SimpleXMLRPCServer def add(x,y): return x+y server=SimpleX ...
- Html5 Egret游戏开发 成语大挑战(五)界面切换和数据处理
经过前面的制作,使用Egret的Wing很快完成了开始界面和选关卡界面,下面通常来说就是游戏界面,但此时界面切换和关卡数据还没有准备好,这次讲解界面的切换和关卡数据的解析.前面多次修改了Main.ts ...
- [转]使用URLDecoder和URLEncoder对中文进行处理
一 URLEncoder HTML 格式编码的实用工具类.该类包含了将 String 转换为 application/x-www-form-urlencoded MIME 格式的静态方法.有关 HTM ...
- MVC-RedirectToAction跳转到其他Area
mvc使用Area分区开发后,存在不同Area之间的跳转,需要为每个区间添加Area规则,如下: using System.Web.Mvc; namespace web.Areas.FrameSet ...
- python 图
class Graph(object): def __init__(self,*args,**kwargs): self.node_neighbors = {} self.visited = {} d ...
- 完全背包变型题(hdu5410)
这是2015年最后一场多校的dp题,当时只怪自己基础太差,想了1个多小时才想出来,哎,9月份好好巩固基础,为区域赛做准备.题目传送门 题目的意思是给你n元钱,m类糖果,每类糖果分别有p, a, b, ...
- MFC下debug改成release版本出现问题及解决办法
自己在debug下成功运行了自己写的测试自己写第三方库的程序,这里有用到opencv库,所以同时用到了自己的库和opencv的库,需求因为要进行速度的测试,是想要把debug改成release版本,这 ...
- ASP.NET MVC 关闭 客户端 xss 检查
为防止 XSS 攻击,asp.net 机制 会默认检测 请求报文 内是否有包含html标签,以提醒开发人员处理,报错如下:"从客户端中检测到有潜在危险的Request...值"当我 ...
- 将DBF文件导入Sqlserver数据库
项目中的问题:用户选择N个dbf文件导入sql2005数据库,由于每年dbf表结构都在变化,所以在sql2005中根本就不存在,需要每年根据dbf的结构自动建表.(文章来自http://blog.cs ...
- 准备.Net转前端开发-WPF界面框架那些事,UI快速实现法
题外话 打开博客园,查看首页左栏的”推荐博客”,排名前五的博客分别是(此处非广告):Artech.小坦克.圣殿骑士.腾飞(Jesse).数据之巅.再看看它们博客的最新更新时间:Artech(2014- ...