用加减法模拟除法。

除法本质就是 被除数 - 商个除数相加 = 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. ios专题 - sandbox机制

    [原创]http://www.cnblogs.com/luoguoqiang1985 ios在安装APP时,把APP的偏好设置与数据放在sandbox里.sandbox通过一系列细颗粒度控制APP访问 ...

  2. 用3种方法在 operator= 中处理“自我赋值”

    假设你建立一个class 用来保存一个指针指向一块动态分配的位图. class Bitmap {......}; class Widget{ ... private: Bitmap* pb ; }; ...

  3. C++资源之不完全导引 (转载)

    C++资源之不完全导引(完整版)- - 这文章太强了,我一定要转载,否则对不起观众,对不起自己.(liigo) 发信人: NULLNULL (空空), 信区: VC标  题: C++资源之不完全导引( ...

  4. Java学习----有风险的代码(异常)

    Exception继承了Throwable,但是它本身是有异常类的父类. RuntimeException:运行时异常 Exception->RuntimeException->NullP ...

  5. WPF AutoGeneratingColumn 绑定下拉框

    WPF自动产生列,前台代码: <DataGrid x:Name="Dg" AutoGenerateColumns="True" CanUserAddRow ...

  6. demo_02 less

    html 中的代码<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  7. new Image()的用途

    new Image()用途总结: 1.图片预加载      在做游戏时,为了使图片能快打开可以做预加载.      原理:创建image对象,将image对象的src分别指向需加载的图片地址,图片被请 ...

  8. php 删除语句

    if($query&&mysql_affected_rows())echo('数据已被删除');else echo('错误,无法删除'); 通过返回影响的行数 来判断是否已经删除

  9. Mac系统安装Lua(转)

    下载最新版的lua请点击,然后解压 运行“终端”进入到该文件夹下 ,主要是cd [文件夹名] 在“终端”输入 make macosx (回车) 在“终端”输入 make test (回车) 然后再输入 ...

  10. MySQL创建用户、授权等

    用于MySQL5.6命令行执行成功: create database wp_people; create user wp_people@'localhost' identified by 'yrwb' ...