用加减法模拟除法。

除法本质就是 被除数 - 商个除数相加 = 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. 283. Move Zeroes(C++)

    283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...

  2. C#应用程序中读取Oracle数据库

    前言 最近的任务就是开发了一个功能,要从供应商那边读取数据,然后拿过来,处理以后放到我们自己的数据库中.供应商那边是Oracle数据库,其实不管什么数据我想都差不多,于是我就开始了.由于在家里写的博客 ...

  3. PHPExcel导出excel

    如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把$yourStr从utf-8转换成了gb2312: $yourStr = mb_convert_encoding("g ...

  4. windows通过Composer安装yii2

    1. php.ini 中;extension=php_openssl.dll(取消注释,不然在安装composer过程中会报错) 集成环境最好去php目录中打开php.ini文件,确定;extensi ...

  5. (MVC)验证用户是否登录 登录认证

    验证类 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  6. Apache与Nginx的比较

    1.Apache与Nginx的优缺点比较 nginx相对于apache的优点: 轻量级 : 同样起web 服务,比apache 占用更少的内存及资源 抗并发 : nginx 处理请求是异步非阻塞的,而 ...

  7. tyvj P1716 - 上帝造题的七分钟 二维树状数组区间查询及修改 二维线段树

    P1716 - 上帝造题的七分钟 From Riatre    Normal (OI)总时限:50s    内存限制:128MB    代码长度限制:64KB 背景 Background 裸体就意味着 ...

  8. VC++6.0 编写插件(图文并茂)

    下午偶然注意到VC++6.0新建工程标签页下的DevStudio Add-in Wizard,没有接触过,看名字是给Developer Studio开发插件,心生喜感,于是百度之,发生百度检索几乎找不 ...

  9. Java 比较两个字符串的大小

    比较两个字符串的大小 static int compareTo(String s1, String s2) { int len1 = s1.length(); int len2 = s2.length ...

  10. 深入浅出Node.js (附录C) - Node编码规范

    C.1 根源 C.2 编码规范 C.2.1 空格与格式 C.2.2 命名规范 C.2.3 比较操作 C.2.4 字面量 C.2.5 作用域 C.2.6 数组与对象 C.2.7 异步 C.2.8 类与模 ...