实现两个整数的除法,不许用乘法、除法和求模。题目被贴上了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 位运算实现整数除法的更多相关文章

  1. LeetCode: Divide Two Integers 解题报告

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...

  2. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  3. leetcode:single-number-ii(Java位运算)

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  4. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  6. Leetcode:Divide Two Integers分析和实现

    题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...

  7. leetcode Single Number II - 位运算处理数组中的数

    题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...

  8. [LeetCode] Single Number II 位运算

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  9. leetcode Divide Two Integers python

    class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...

随机推荐

  1. 九、Android学习笔记_ Android开发中使用软引用和弱引用防止内存溢出

    在<Effective Java 2nd Edition>中,第6条“消除过期的对象引用”提到,虽然Java有 垃圾回收机制,但是只要是自己管理的内存,就应该警惕内存泄露的问题,例如的对象 ...

  2. Java之奇偶组合

    写一个函数,将已知数组的奇数项组合成一个新的数组,在函数中调用该数组,并且输出新数组的内容. 定义一个数组,该数组为{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ...

  3. 在调用“Fill”前,SelectCommand 属性尚未初始化

    在调用“Fill”前,SelectCommand 属性尚未初始化 是因为少写了一行代码: private readonly string strConnection = System.Configur ...

  4. windows 7 打开控制面板的命令

    在开始菜单中键入contrl,回车就进入了控制面板,比较快

  5. UI1_UITableViewHomeWork

    // // AppDelegate.m // UI1_UITableViewHomeWork // // Created by zhangxueming on 15/7/14. // Copyrigh ...

  6. CSS样式表与格式布局

    样式表 CSS(Cascading Style Sheets  层叠样式表),作用是美化HTML网页. 内联样式表: 例:<p style="font-size:10px;" ...

  7. python——类

    类的定义格式: class CLASSNAME(父类1,父类2,父类3): __A  = 0     ##私有属性,两个_开头,只有在该类中的函数才能访问 B = 0 ##公共属性 def 函数名(s ...

  8. TextEdit验证

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using ...

  9. WP开发笔记——去除 HTML 标签

    获取到一段HTML类型的信息,显示在WP的webbrowser控件中,如果不加处理的话,会显示出各种神烦的HTML标签. 这时,需要我们将这HTML类型的信息进行处理去除HTML标签后再显示出来,这里 ...

  10. nginx安装总结

    对于nginx作为负载均衡服务器时的安装需要安装rewrite模块需要的pcre()库,gzip模块需要zlib库,ssl模块需要openssl库,对此依赖安装有很多种处理方式,以下简单总结: 通过源 ...