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. 注意−231取绝对值后会溢出
class Solution {
public int divide(int dividend, int divisor) {
//handle overflow
int res = 0;
if(dividend==Integer.MIN_VALUE)
{
if(divisor==-1)
return Integer.MAX_VALUE;
res = 1;
dividend += Math.abs(divisor);
}
if(divisor==Integer.MIN_VALUE)
return res; //handle negative
Boolean isNeg = false;
if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) isNeg = true;
dividend = Math.abs(dividend);
divisor = Math.abs(divisor); int cnt = 0; //记录divisor左移的次数
//找到商的最高比特位
while(divisor <= (dividend >> 1)){ //除数与被除数/2相比,为了保证除<被除数,且防止溢出
divisor <<= 1;
cnt++;
} //从最高比特位开始求出商的每一位
while(cnt >= 0){
if(dividend >= divisor){ //dividend > divisor说明当前比特位为1,否则为0
dividend -= divisor;
res += 1 << cnt;
}
divisor >>= 1;
cnt--;
} return isNeg?-res:res;
}
}

通过二进制位移完成乘除,每次左移一位,相当于十进制中*2;每次右移一位,相当于十进制中/2

将除数位移至小于被除数的最大值,以获取商的最高位

之后除数每右移一位,求商的后一位。用被除数-除数,如果>=0,说明该二进制位的值位1,反之则为0

29. Divide Two Integers (JAVA)的更多相关文章

  1. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  2. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  3. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

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

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

  5. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

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

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

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

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

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

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

  9. 29. Divide Two Integers (INT; Overflow, Bit)

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

随机推荐

  1. 公司-半导体:Micron

    ylbtech-公司-半导体:Micron 美光科技有限公司(Micron Technology, Inc.)是高级半导体解决方案的全球领先供应商之一.通过全球化的运营,美光公司制造并向市场推出DRA ...

  2. 廖雪峰Java9正则表达式-1正则表达式入门-1正则表达式简介

    1.使用代码来判断字符串 场景: 判断字符串是否是有效的电话号码:"010-12345678", "123ABC456" 判断字符串是否是有效的电子邮箱地址:& ...

  3. 05-Eclispe配置Tomcat插件

    此插件只针对 eclipse-java-indigo-SR2-win32 这个开发工具使用的 1.下载tomcat插件 2.解压到指定位置 3.找到eclispe安装目录 D:\software\ec ...

  4. adb相关指令 笔记

      adb相关指令 笔记 1.adb devices 查看物理测试设备或模拟器的相关信息,有三个状态: (1)device 设备已连接到adb服务器上,但该状态并不代表设备已启动完毕可以进行操作: ( ...

  5. Linux配置虚拟内存

    我的Linux内存很少,所以我给它弄个虚拟内存 首先建立一个1G的空文件: dd if=/dev/zero of=/home/swapfile bs=64M count=16 格式化为swap: mk ...

  6. hanlp使用自定义词典抽取关键词

    1.在data/dictionary/custom/路径下新建文件 myDict.txt.,添加新的单词,单词,词性,词频.并删除当前文件夹下的bin文件, 2.在hanlp配置文件中的CustomD ...

  7. .NET Core 中的通用主机和后台服务

    简介 我们在做项目的时候, 往往要处理一些后台的任务. 一般是两种, 一种是不停的运行,比如消息队列的消费者.另一种是定时任务. 在.NET Framework + Windows环境里, 我们一般会 ...

  8. Java JTable列顺序和列宽度保存在用户本地

    上周碰到了一个棘手的需求,就是要把用JTable的列顺序和列宽度保存下来,这次用户调整了列宽度,关闭程序,下次再打开时,这个列的宽还是要保持,因为SWing的特性,都是在程序启动时就确定了列顺序和列宽 ...

  9. stm32 HAL库笔记(一)——串口的操作

    昨天分析了普通io口的使用,和初始化代码流程,回顾一下,首先定义一个配置io口功能的结构体,然后开启时钟,再去配置这个结构体里面的各个成员变量,每个成员变量都有很多种选择,可以看各个成员变量 后面的注 ...

  10. 啊哈算法第四章第二节解救小哈Java实现

    package corejava; public class FourTwo { static int m;//(m,n)为几行几列 static int n; static int p;//(p,q ...