用加减法模拟除法。

除法本质就是 被除数 - 商个除数相加 = 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. [转载] java中byte数组与int,long,short间的转换

    文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...

  2. Django练习项目之搭建博客

    背景:自从今年回家过年后,来到公司给我转了试用,我的学习效率感觉不如从前,而且刚步入社会我总是想要怎么想明白想清楚一些事,这通常会花掉,消耗我大量的精力,因为我想把我的生活管理规划好了,而在it技术学 ...

  3. php函数应用场景

    截取文件后缀: $slen = strlen($suffix); substr($this->uri_string, -$slen) === $suffix 原理:截取倒数多少长度字符 判断多维 ...

  4. adodb配置与使用

    =========================================php100:80:ADODB PHP数据库万能引擎类 ADODB PHP数据库介绍与特点 ADODB 是一种兼容的各 ...

  5. In Place Upgrade of CentOS 6 to CentOS 7

    Note: This is not the most highly recommended method to move from CentOS 6 to CentOS 7 ... but it ca ...

  6. failure injection

    (1)malloc穷尽的情况: 假设下面的代码是测试代码,里面含有被测函数fmalloc,其中含有一个malloc语句,在一般情况下,是很难走到malloc失败的分支的,因为很难模拟系统内存耗尽的情况 ...

  7. WebApi学习总结系列第三篇(Http)此篇持续更新...

    越了解Http对WebApi开发就越有帮助,因为WebApi就是建立在Http基础之上的. 一.Http: 通过 <ASP.NET Web API 2 框架揭秘>一书中 了解到 什么叫We ...

  8. js中的字符串

    JS里并没有标准的多行字符串的表示方法,但是在用模板的时候,为了保证模板的可阅读性,我们又不可避免的使用多行字符串,所以出现了各种搞法,这里以一段jade的模板作为示例,简单总结和对比一下. 字符串相 ...

  9. Spring 中设置依赖注入

    package com.ysq.vo; public class User { private int uid; private String uname; private String pwd; p ...

  10. uva 10130 SuperSale

    一个01背包问题: 刚刚开始把题目看错了,以为物品的数目是有限的,然后让你求一个家庭里最多能够拿多个价值的东西: 这样一来的话,这个题目就有点意思了: 但是后来发现竟然是个简单的01背包问题 =  = ...