实现两个整数的除法,不许用乘法、除法和求模。题目被贴上了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. openquery链表删除时报错 “数据提供程序或其他服务返回 E_FAIL 状态”

    DELETE OPENQUERY (VERYEAST_COMPANY_MYSQL_CONN, 'SELECT * FROM company ') WHERE c_userid in(select c_ ...

  2. asp.net运算符之逻辑运算符以及其他运算符

    逻辑(布尔型)运算符用于对boolean型的结果的表达式进行运算,运算的结果都是boolean型.其运算结果如下所示: 运算符 运算 例子 结果 & AND(与) false&true ...

  3. 零基础Android学习笔记-03 窗口间的数据传递

    1.通过全局变量来传递. 新建一个全局的类继承于Application package com.example.helloworld; import android.app.Application; ...

  4. raphael画图

    // 在坐标(10,50)创建宽320,高200的画布 var paper = Raphael(10, 50, 320, 200); // 在坐标(x = 50, y = 40)绘制半径为 10 的圆 ...

  5. jQuery异步分页插件

    学校软件工程让写课程设计(其实就是自选语言做个项目),感觉都是重复的东西就没有很认真的去写内容,更加注意写一些之前没有用过的东西. 因为一直都使用TP框架来写PHP,TP又自带分页类,想到这里就想试试 ...

  6. UI2_ScrollView&UIPageControl

    // // ViewController.h // UI2_ScrollView&UIPageControl // // Created by zhangxueming on 15/7/10. ...

  7. (转)Linux概念架构的理解

    英文原文:Conceptual Architecture of the Linux Kernel 摘要 Linux kernel成功的两个原因:(1)架构设计支持大量的志愿开发者加入到开发过程中:(2 ...

  8. multiple backgrounds 多重背景

    语法缩写如下: background : [background-color] | [background-image] | [background-position][/background-siz ...

  9. JavaScript 中的 replace 方法

    定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replaceme ...

  10. ipc telnet 攻击

    ping %1 -n 2net use \\%1sc \\%1 config tlntsvr start= autosc \\%1 start tlntsvrtelnet %1