用加减法模拟除法。

除法本质就是 被除数 - 商个除数相加 = 0

如果你电脑足够好,可以无限减。。但是这个题肯定不是这么简单。

最快的方法还是 减去 商乘以除数。

但是这里不能使用乘法,那只好用BIT的运算来实现了。

自己没做出来,但是发现一刷做出来了,怎么看都不像是我这个智商能写出来的,所以不知道当时看的哪的答案,贴出的答案如有冒犯,请告之。

比如19/3,只要分子比分母大,就可以除。

19 比 3大, 比2个还3大,牛逼牛逼,比4个3大,你敢信?比8个3小。。

那么19先减去4个3肯定没错,此时分子是19-4*3=7;结果是4,已经4个3了。

然后再看7/3,7比1个3大,比2个3大,那么分子就是7-2*3 = 1,结果是2+刚才的4=6。

然后1/3,比3小了,让你装逼。此时停止,结果是第一次的2+刚才的4=6.

所以就是通过BIT位操作来看剩下的分子比几个分母大,正常乘法是1234567个这么测试,测1次就行了,但是用BIT是测很多次,每次1248个,最后相加。

然后各种edge case好贱。

public class Solution {
public int divide(int dividend, int divisor)
{
if(dividend == 0) return 0; long up = (long)dividend;
long down = (long)divisor; boolean pos = (up*down) >= 0;
up = Math.abs(up);
down = Math.abs(down); long res = 0;
while(up >= down)
{
int numOfDowns = 1; while(up > (down<<1))
{ down <<= 1;
numOfDowns <<= 1; } up -= down;
res += (long)numOfDowns;
down = Math.abs((long)divisor);
} if(res == (long)Integer.MIN_VALUE*(-1) && pos) return Integer.MAX_VALUE;
if(pos) return (int)res;
else return (int)res*-1;
}
}

29. Divide Two Integers的更多相关文章

  1. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  2. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  3. [LeetCode] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  4. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

  5. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  6. 29. Divide Two Integers (JAVA)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  7. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  8. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  9. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  10. 29. Divide Two Integers (INT; Overflow, Bit)

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

随机推荐

  1. linux下安装svn(基于编码的方式)

    svn是什么,相信能看到这里的同学应该不会有这个问题了,费话不多说,开始: 1.创建目录 mkdir /home/svn/ 2.获取安装svn所需源文件(svn的官方网址是http://subvers ...

  2. 经典SQL练习题

    题目地址:http://blog.csdn.net/qaz13177_58_/article/details/5575711 1. 查询Student表中的所有记录的Sname.Ssex和Class列 ...

  3. WPF自定义窗口(Windows Server 2012 Style)

    先上图 新建CustomControl,名:HeaderedWindow Themes\Generic.aml文件夹下加入 笔刷,转换器 <SolidColorBrush x:Key=" ...

  4. js 中对象属性的特性

    数据属性: 数据属性包含一个数据值的位置,在这个位置可以读取和写入值. 4个描述的行为特性: writable  表示能否修改属性的值.默认为true Enumerable 表示能否过过for in循 ...

  5. AS3.0定义变量的访问范围

    在AS3.0中变量的默认访问范围是:internal:包内成员可以访问,包外不可访问.AS2.0默认访问范围是public

  6. __isset()检测类内部变量是否设置

    __isset()--检测类内部私有变量是否存在 当执行isset方法时自动执行 class Per{ private $name; private $age; function __construc ...

  7. php基础知识【函数】(1)数组array

    一.排序 1.sort -- 从最低到最高排序,删除原有的键名,赋予新的键名[字母比数字高] 2.rsort -- 逆向排序(最高到最低),删除原有的键名,赋予新的键名[字母比数字高] 3.asort ...

  8. new作为修饰符

    new 修饰符与 new 操作符是两个概念 new 修饰符用于声明类或类的成员,表示隐藏了基类中同名的成员.而new 操作符用于实例化一个类型 new 修饰符只能用于继承类,一般用于弥补基类设计的不足 ...

  9. HierarchyView的实现原理和Android设备无法使用HierarchyView的解决方法

    声明:由于本人一直用eng版的真机调试,所以此方法没有用过,记录在这里,有机会验证 ---------------------------------------------------------- ...

  10. ERROR 2003 (HY000): Can't connect to MySQL server

    http://blog.csdn.net/longxibendi/article/details/6363934 一.问题的提出 /usr/local/webserver/mysql/bin/mysq ...