397. Integer Replacement
先正统做法。
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的更多相关文章
- LeetCode 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- Week3 - 397. Integer Replacement
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 397 Integer Replacement 整数替换
给定一个正整数 n,你可以做如下操作:1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是多少?示例 ...
- [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 ...
- [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 ...
- LeetCode——Integer Replacement
Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...
- Integer Replacement
https://leetcode.com/problems/integer-replacement/#/solutions 这题是一道典型的搜索问题,我采用广度搜索,可以直接输出最短路径.这题的tes ...
随机推荐
- DIV+CSS 网页布局之:混合布局
1.混合布局 在了解了一列.两列和三列布局之后,混合布局也就不难理解了,混合布局也可以叫综合型布局,那么混合布局就可以在一列布局的基础之上,分为两列布局,三列布局,网页布局的结构普遍都是三列布局,但是 ...
- php实现手机拍照上传头像功能
现在手机拍照很火,那么如何使用手机拍照并上传头像呢?原因很简单,就是数据传递,首先手机传递照片信息,这个就不是post传递 也不是get函数传递, 这个另外一种数据格式传递,使用的是$GLOBALS ...
- 在ubuntu12.0.4上搭建samba服务器以实现文件共享
在安装之前samba服务器之前,先进行以下配置和测试. <壹> 准备工作 一.NAT联网方式 (1)硬件连接 无需网线,无需路由器 (2)虚拟机选择NAT连接方式 (3)测试网络通不通 在 ...
- Windows server2008 搭建ASP接口访问连接oracle数据库全过程记录--备用
真的是太不容易了,以前的时候在window server 2003上面搭建了一套asp+oracle的接口系统,就费了好大的劲儿,其实那会迷迷瞪瞪的也不知道怎么的就弄好了,也懒得管了.OK,从昨天到今 ...
- bzoj 3781: 小B的询问 分块
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solved: 135[Submit][Status] Descrip ...
- Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights
一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...
- [QuickX]xcode运行Quick-cocos2d-x项目时自动更新lua资源文件
1.项目设置 build settings ->build options ->Scan all source files and Includes = YES 2.加入script (1 ...
- 【HDOJ】2206 IP的计算
题目很简单,情况有很多种. #include <stdio.h> #include <string.h> ]; int isIPaddr(char buf[]) { int i ...
- BZOJ2105: 增强型LCP
2105: 增强型LCP Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 366 Solved: 86[Submit][Status] Descrip ...
- 浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案
作者 王枫发布于2014年2月19日 综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个 ...