【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法、除法和求模。题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系。我想的方法也和BS一点关系都没有。
很早以前我就猜想,整数的乘法是不是总是可以用移位和加法来实现?当然可以了,任何整数都可以写成2n或2n+1的形式,移位就是那个乘以2,加法就是最后的+1了嘛。复杂度是O(1),因为整数的移位最多32次,因此在循环中移位的次数也极其有限。
例如123/5:
5 123
<<1
<<1
<<1
<<1
5*16 = 80
5 123-80=43
<<1
<<1
<<1
5*8 = 40
5 43-40=3
5<3, thus result = 16+8 = 24
(if dividend == 125, finally we have:
5==5, thus result = 16+8+1 = 25;)
于是:
int divide(long long a, long long b) {
if(b==) return a>=?0x7fffffff:0x80000000;
if(a==) return ;
if(b==) return a;
int sgn = (a^b) & 0x80000000; //0 -> +, 0x80000000 -> -
if(a<) a=-a;
if(b<) b=-b;
//a,b>0
if(a<b) return ;
int res = , _b = b, twon;
while(a>b)
{
twon = ;
while(a>b)
{
b<<=;
twon<<=;
}
b>>=;
twon>>=;
a -= b;
b = _b;
res += twon;
}
if(a==b) res++;
if(sgn)
return -res;
else return res;
}
Leetcode上测试时间为36ms。
这里玩了一个小trick,用long long而不是int来存变量了,主要是防止被除数太大时除数移位会溢出变成负数。当然去掉long long也不难啦,加上溢出的判断就好了。只不过,这里的溢出判断和函数atoi是有所不同的,atoi中的result每次乘以10再加上0-9,而这边是乘以2。以上。
【Leetcode】 - Divide Two Integers 位运算实现整数除法的更多相关文章
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- leetcode:single-number-ii(Java位运算)
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- leetcode Single Number II - 位运算处理数组中的数
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
- [LeetCode] Single Number II 位运算
Given an array of integers, every element appears three times except for one. Find that single one. ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
随机推荐
- Oracle学习笔记4 使用Navicat for Oracle 连接Oracle时出现错误:ORA-28547: connection to server failed, probable Oracle Net admin error
出问题到的机器环境: Oracle 11gR2 64bit Navicat for Oracle 11.0.10 根据网上一些大神的做法及个人的一些推测,总结如下: 问题出现的原因:Navicat与O ...
- 使用命令修改ip地址
简述:以serverv 2012 r2为例 常用的几种,当然不全,希望能较快的速率记下一种便可 直接配置 1. 查看网卡的显示名称 2. 配置静态iP地址 3. 查看配置 ...
- Cocos2d-JS中的文本菜单
文本菜单是菜单项只能显示文本,文本菜单类包括了cc.MenuItemLabel.cc.MenuItemFont和cc.MenuItemAtlasFont.cc.MenuItemLabel是个抽象类,具 ...
- 20150225--ASP.NET基础(2)
一.引用外部样式表或JS,在<head>-</head>标签之间添加: <link href="yangshi.css" type="tex ...
- 8款超酷的HTML5 3D图片动画源码
1.HTML5移动端图片左右切换动画 今天要给大家分享一款很不错的图片左右切换焦点图动画,并且支持移动端触摸滑动.功能上,这款HTML5图片播放器支持鼠标滑动.手机端触摸滑动以及自动播放.外观上,这款 ...
- 非常难得的iPad版房地产售楼助手应用
一款高质量的iPad房地产售楼助手应用,采用的是类似facebook,新浪微博,腾讯微博,人人网的布局视图.功能有:客户管理系统(可添加,编辑等):2.房源管理系统;3.房贷计算器等,这个应用无论是布 ...
- 如何判断一个js对象是不是Array
1. instance of 2.constructor 3. isArray 1.var a=new Array(); a instanceof Array; //true 2.var a=new ...
- 《Linux系统 date、cal、hwclock时间命令的用法》
date命令的用法: [root@apache ~]# date //查看当前系统的时间 Sat Jun 14 13:46:02 CST 2014 [root@apache ~]# date -s & ...
- nignx+php-fpm环境下 phpmyadmin打开空白的原因探究
打开phpmyadmin一直是空白的,发现是js的问题,原因是pma的js/get_script_js.php读取js不完整 很容易的将问题原因想到了php的输出缓存大小上,我把php.ini里的ou ...
- SQLite简易入门
本文内容来源:https://www.dataquest.io/mission/129/introduction-to-sql 本文所用数据来源:https://github.com/fivethir ...