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

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

题目要求不使用乘除和取余的运算得到除法的结果,我们第一个想到的肯定是减法,除法的本质就是减法嘛,但是减法的时间复杂度很不友好,如果被除数是Integer.MAX_VALUE,除数是1,那就很麻烦了

在这个基础上我们想到了更高级一点的操作方法,位移运算,<<1表示把字节左移一位相当于*2,我们不断扩大除数divisor,直到它再扩大于被除数,这个时候我们需要缩小被除数,就用这个被除数减去当前的除数然后再重复之前的过程,就得到了我们想要的结果。这里需要注意的是测试样例存在integer.MAX_VALUE和-1的特殊情况,所以我们要使用long来帮助存储判断

class Solution {
public int divide(int dividend, int divisor) {
long m=Math.abs((long)dividend);
long n=Math.abs((long)divisor);
long res=0,tag=Integer.MAX_VALUE;
if(m<n) return 0;
while(m>=n){
long temp=n,count=1;
while(m>(temp<<1)){
temp<<=1;
count<<=1;
}
m-=temp;
res+=count;
}
if((dividend<0)^(divisor<0)) res=-res;
return (int)(res>tag ? tag:res);
}
}

[LeetCode]29. Divide Two Integers两数相除的更多相关文章

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

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

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

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

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

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

  4. [LeetCode] Divide Two Integers 两数相除

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

  5. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  6. [LintCode] Divide Two Integers 两数相除

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

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

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

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

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

  9. 【LeetCode每天一题】Divide Two Integers(两整数相除)

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

随机推荐

  1. 使用python-docx生成Word文档

    首先是安装python-docx:(centos环境) pip install python-docx 基本方法使用: from docx import Document from docx.shar ...

  2. leecode刷题(6)-- 两个数组的交集II

    leecode刷题(6)-- 两个数组的交集II 两个数组的交集II 描述: 给定两个数组,编写一个函数来计算它们的交集. 示例: 输入: nums1 = [1,2,2,1], nums2 = [2, ...

  3. HTTP上下文表单内容转为实体对象

    using ServiceStack.Web; using System; using System.Collections.Generic; using System.Linq; using Sys ...

  4. CentOS中源码编译安装Nginx

    1.从官网通过wget命令将Nginx源码包下载到Linux上 ​ Nginx下载页面:http://nginx.org/en/download.html 2.在Linux上安装需要依赖的环境 yum ...

  5. 如何把win10系统迁移到SSD固态硬盘

    https://jingyan.baidu.com/article/5d368d1ec59ac43f60c05733.html 我之前将两个盘都已经固定在笔记本内,迁移完之后无论怎么改还是从原来的机械 ...

  6. Jupyter 安装与应用

    用pip安装Jupyter pip install jupyter 从命令行启动笔记本服务器 jupyter notebook 前提要先启动python,这里有一个 token值,如果不是使用默认浏览 ...

  7. 获取HTML代码用 像阿里巴巴

         public string GetHtml(string url)         {               string html = String .Empty;          ...

  8. 鼠标拖动div,div跟随鼠标移动效果

    <div id="boxDiv" style='width:20px;height:20px;position:absolute;background:red;'>   ...

  9. FLUENT 流体计算应用教程

    温正 清华大学出版 2013.1                                          子谓颜渊曰,用之则行,舍之则藏,惟我与尔有是夫!         非常合适的一本书. ...

  10. 1136 A Delayed Palindrome (20 分)

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...