先正统做法。

public class Solution {
public int integerReplacement(int n)
{
if(n == 1) return 0;
int res = 0;
while(n != 1)
{
if(n % 2 == 0)
{
n/=2;
res++;
}
else return res + odd(n); } return res;
} public int odd(int n)
{
return Math.min(integerReplacement(n+1),integerReplacement(n-1))+1;
}
}

结果TLE

考虑下别的做法。

不管是/2 +1 -1都是1 bit操作,从bit上入手。

8 = 1 0 0 0 需要右移3次。

9 = 1 0 0 1 -1 /2 /2 /2

基本思路是;

如果是偶数,最右位(RMB)是0,直接/2;

如果是奇数,通过+ -去掉RMB的1. 但是+ -还是有学问的, 比如 1111 和 1001,第一个明显是+1好,第二个是-1好。

这里+1 -1的目的是为了shift right服务的,所以标准就是通过+1还是-1去掉的1越多越好。用brute-force就是都试试,然后计算机替你选择一个。作为人类,我们要发现规律。。。

所以就是能/2就/2,不能就数+1 -1哪个划算;哪个划算通过看哪个去掉1的个数多决定。其实-1只有在右起第二位是0的情况下划算(还有11的时候,这他妈是个大坑)。

public class Solution {
public int integerReplacement(int n)
{ int res = 0;
while(n != 1)
{
if(n % 2 == 0) n/=2;
else if( n == 3 || ((n>>1) & 1) == 0) n--;
else n++; res++; } return res;
} }

一开始在3上遇到大坑,拿3验证发现他妈不是那么回事啊。。结果3是个特例,因为对于3来说,通过+1去掉2个1,不如-1来的实在。。掉这个坑里一次。

然后另外就是,居然还是TLE... 有没有搞错啊。。

后来发现是各种运算都要换成bit的才行,/2 %2之类的。。

public class Solution {
public int integerReplacement(int n)
{ int res = 0;
while(n != 1)
{
if((n & 1) == 0) n >>>=1;
else if( n == 3 || ((n>>>1)&1)==0) n--;
else n++; res++; } return res;
} }

貌似因为>>>不需要判断符号,所以比>>要快? >>>换成 >>就TLE

求高人指点。

397. Integer Replacement的更多相关文章

  1. LeetCode 397. Integer Replacement

    397. Integer Replacement QuestionEditorial Solution My Submissions   Total Accepted: 5878 Total Subm ...

  2. Week3 - 397. Integer Replacement

    Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...

  3. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  4. 397 Integer Replacement 整数替换

    给定一个正整数 n,你可以做如下操作:1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是多少?示例 ...

  5. [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 ...

  6. 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 ...

  7. [Swift]LeetCode397. 整数替换 | Integer Replacement

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  8. LeetCode——Integer Replacement

    Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...

  9. Integer Replacement

    https://leetcode.com/problems/integer-replacement/#/solutions 这题是一道典型的搜索问题,我采用广度搜索,可以直接输出最短路径.这题的tes ...

随机推荐

  1. BOM 子对象,history,location,screen

    history:包括浏览器访问过的 url 属性:返回浏览器访问过的历史记录数 方法:back(); forward(); go(number) location:包含当前 url 的相关信息 属性: ...

  2. ubuntu远程登陆windows

    首先安装rdesktop : apt-get install rdesktop.p 程序安装完后,在终端命令行中输入:$ rdesktop -g 1024x768 -d 24 ip,就进入了windo ...

  3. Linux命令 &与&&的作用

    1.ls &表示后台服务 2.ls && ll 表示前者执行成功,执行后台命令

  4. linux 配置apache+subversion

    http://apr.apache.org/download.cgi http://subversion.tigris.org/servlets/ProjectDocumentList?folderI ...

  5. javascript第二遍基础学习笔记(一)

    1.兼容xhtml方法: <script> //<![CDATA[ ... ... //]]> </script> 2.文档模式: IE5.5引入,最初包含2种:混 ...

  6. Struts, Namespace用法

    最近在用SSH框架做一个项目,在使用Struts 的namespace时遇到不少问题,现在就对struts namespace 做一个简单的介绍吧.(本文从项目结构展开叙述) (第1次写博客, 写的不 ...

  7. [OI笔记] 最长上升子序列与网络流建模

    与最长上升子序列相关的网络流问题: 给定一个序列 A[1..n] ,求出 A 的最长上升子序列长度.并且回答下列询问: (1) 如果每个点只能用一次,能从 A 中取出几个最长上升子序列? (2) 如果 ...

  8. Logstash同步Oracle数据到ElasticSearch

    最近在项目上应用到了ElasticSearch和Logstash,在此主要记录了Logstash-input-jdbc同步Oracle数据库到ElasticSearch的主要步骤,本文是对环境进行简单 ...

  9. SecureCRT 绝佳配色方案, 保护你的眼睛

    http://blog.csdn.net/zklth/article/details/8937905   关键词:SecureCRT配色, SecureCRT设置颜色, Linux终端配色,Linux ...

  10. 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)

    [题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...