先正统做法。

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. HTML标签总结

    HTML 基本文档 <!DOCTYPE html> <html> <head> <title>文档标题</title> </head& ...

  2. Git 的基本配置

    用户信息 你个人的用户名称和电子邮件地址,用户名可随意修改,git 用于记录是谁提交了更新,以及更新人的联系方式. $ git config --global user.name "Donl ...

  3. java 各种排序算法

    各种排序算法的分析及java实现   排序一直以来都是让我很头疼的事,以前上<数据结构>打酱油去了,整个学期下来才勉强能写出个冒泡排序.由于下半年要准备工作了,也知道排序算法的重要性(据说 ...

  4. OS概论2

    实时系统 实时即表示及时,实时计算可以定义为这样一类计算:系统的正确性,不仅由计算的逻辑结果来确定,而且还取决于产生结果的时间.事实上,实时系统最主要的特征,是将时间作为关键参数,它必须对所接收到的某 ...

  5. Nginx+uWSGI+Django+Python在Linux上的部署

    搞了一整天,终于以发现自己访问网络的端口是错误的结束了. 首先要安装Nginx,uWSGI,Django,Python,这些都可以再网上查到. 安装好后可以用 whereis 命令查看是否安装好了各种 ...

  6. WPF窗体置于桌面最底层

    在WPF中设置窗体的Topmost属性可以将窗体永远置于顶部,但是没有提供Bottommost属性将窗体置底.若果要将窗体置于桌面的最底部,就需要使用Windows API来实现了.解决方案如下: 1 ...

  7. c#反射机制判断同一个类的两个实例的值是否完全一样

    ; i < properties1.Length; i++)            {                string s = properties1[i].DeclaringTyp ...

  8. STM32库函数开发使用总结

    一.外设常具备的几类寄存器 控制寄存器xxx_CR (Control/Configuration Register): 用来配置.控制响应外设的工作方式,如GPIOx_CRL.AFIO_EXTICR1 ...

  9. iphone 与 ipad -- UIPopoverPresentationViewController

    iOS8.0之后, 苹果推出了UIPopoverPresentationViewController, 在弹出控制器时, 统一采用 presentViewController, 但是要实现iPhone ...

  10. jquery仿ios日期时间插件

    Demo下载: 手机时间控件.zip 使用之前,请在页面中加入以下js和css: jquery-1.9.1.js mobiscroll.core-2.5.2.js mobiscroll.core-2. ...