Divide two integers without using multiplication, division and mod operator.

思路:1.先将被除数和除数转化为long的非负数,注意一定要为long。由于Integer.MIN_VALUE的绝对值超出了Integer的范围。

2.常理:不论什么正整数num都能够表示为num=2^a+2^b+2^c+...+2^n。故能够採用2^a+2^b+2^c+...+2^n来表示商,即dividend=divisor*(2^a+2^b+2^c+...+2^n),(a,b,c,....m互不相等。且最大为31,最小为0)。

而商的最大值为Integer.MIN_VALUE的绝对值。商最多有32个2的指数次相加。故时间复杂度为常数。

3.divisor*2^a用计算机表示为divisor<<a;

注意:若每次仅仅加一个divisor。则面对Integer.MAX_VALUE除以一个非常小的常数(eg:1。2。3),会超时。

public class Solution {
public int divide(int dividend, int divisor) { boolean positive = true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0))
positive = false;
long did=dividend>=0?(long)dividend:-(long)dividend;
long dis=divisor>=0?(long)divisor:-(long)divisor; long quotients = positiveDivide(did, dis);
if (!positive)
return (int)-quotients;
return (int)quotients;
} public long positiveDivide(long did, long dis) {
long[] array = new long[32];
long sum = 0;
int i = 1;
long quotients = 0;
if(dis==1) return did;//为了避免-did=Integer.MIN_VALUE,而dis=1。出现故障
for (array[0]=dis; i < 32 && array[i - 1] <= did; i++)
array[i] = array[i - 1] << 1; for (i = i - 2; i >= 0; i--) {
if (sum <= did - array[i]) {
sum += array[i];
quotients += 1 << i;
}
}
return quotients;
}
}

优化版,减小内存的消耗。不申请动态数组

public class Solution {
public int divide(int dividend, int divisor) { boolean positive = true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0))
positive = false;
long did=dividend>=0? (long)dividend:-(long)dividend;
long dis=divisor>=0?(long)divisor:-(long)divisor; long quotients = positiveDivide(did, dis);
if (!positive)
return (int)-quotients;
return (int)quotients;
} public long positiveDivide(long did, long dis) {
long sum = 0;
long quotients = 0;
if(dis==1) return did;//为了避免-did=Integer.MIN_VALUE,而dis=1。出现故障 //sum从divisor*2^31的開始加起,不能加则试试加上divisor*2^30。
//若不能则试试divisor*2^29,依此类推
for (int i = 31; i >= 0; i--) {
long temp=dis<<i;//该式为divisor*2^a //sum<=dividend则说明dividend大于divisor*(2^m+...+2^i),m最大为31
if (sum <= did - temp) {
sum += temp;
quotients += 1 << i;//2^i
}
}
return quotients;
}
}

LeetCode 28 Divide Two Integers的更多相关文章

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

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

  2. Java for LeetCode 029 Divide Two Integers

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

  3. 【leetcode】Divide Two Integers (middle)☆

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

  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两整数相除

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

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

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

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

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

  8. [LeetCode] 29. Divide Two Integers ☆☆

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

  9. 【Leetcode】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. class Solution { public ...

随机推荐

  1. 移动web——bootstrap模板

    基本概念 1.bootstrap就是在媒体查询技术出现以后才开始出现的 2.此技术使响应式开发变得十分轻松,最大特点就是栅格系统(什么设备上如何显示)以及响应式工具(是否可见) 基本模板 <!D ...

  2. html5——渐变

    线性渐变 <style> div { width: 700px; height: 100px; /*方向:从右向左*/ /*起始颜色:黄色*/ /*终止颜色:绿色*/ background ...

  3. 【译】x86程序员手册01

    Intel 80386 Reference Programmer's Manual 80386程序员参考手册 Chapter 1 -- Introduction to the 80386 第1章 - ...

  4. RTL Compiler之synthesis steps

    1 synthesis steps 1) Search Paths rc:/> set_attribute lib_search_path path / rc:/> set_attribu ...

  5. perf-perf stat用户层代码分析

    perf_event 源码分析 前言 简单来说,perf是一种性能监测工具,它首先对通用处理器提供的performance counter进行编程,设定计数器阈值和事件,然后性能计数器就会在设定事件发 ...

  6. 【解题报告】 洛谷 P3492 [POI2009]TAB-Arrays

    [解题报告] 洛谷 P3492 [POI2009]TAB-Arrays 这题是我随机跳题的时候跳到的.写完这道题之后,顺便看了一下题解,发现只有一篇题解,所以就在这里顺便写一个解题报告了. 首先当然是 ...

  7. Aizu - 1379 Parallel Lines

    平行直线 题意:给出一些点,这些点两两相连成一条直线,问最多能连成多少条直线. 思路:暴力出奇迹!!记得当时比赛做这道题的时候一直依赖于板子,结果却限制了自己的思路,这得改.dfs直接暴力,但是需要将 ...

  8. php观察折模式

    <?php class Paper{ private $_observers = array(); public function register($sub){ $this->_obse ...

  9. django访问静态变量的设置

    在项目的urls.py文件中 默认urlpatterns是空的列表需要填入url匹配的路由,默认使用static from django.conf.urls import include, url f ...

  10. 数据类型与变量(Python学习笔记01)

    数据类型与变量 Python 中的主要数据类型有 int(整数)/float(浮点数).字符串.布尔值.None.列表.元组.字典.集合等. None 每个语言都有一个专门的词来表示空,例如 Java ...