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. CDNI - RFC7336翻译

    CDNI框架 摘要 本文档提出了CDNI的一个框架.框架的目的是提供对CDNI问题空间的总体描述,和描述CDN互连所需的各种组件之间的 关系.CDNI需要指定接口和机制解决诸如请求路由,分发交换元数据 ...

  2. Java子类访问父类的私有成员变量

    /**子类会继承父类所有的属性和方法. * 但是根据不同的权限标识符,子类不可见父类的私有变量,但可以通过父类的公共方法访问私有变量 * 所以对于重名变量,子类和父类都各有一份. * 对于子类和父类中 ...

  3. cucumber soapui test web services

    没有UI,所以不需要Selenium. 首先Cucumber: 每个web service形成一个feature文件,描述完成的业务场景. 是否引入参数? 如果引入参数,可能需要根据某种方式保存参数, ...

  4. Oracle表空间不足;查询表空间使用率(unable to extend lob segment SYS_LOB0000076749C00006$$ by 8192 in tablespace USERS)

    查询表空间对应地址 *),) total_space from dba_data_files order by tablespace_name; //方案一:修改表空间大小(32000可改为想要的数值 ...

  5. react基础学习 一

    1. 搭建环境 安装react脚手架  >npm install create-react-app -g 创建文件            >create-react-app 项目名称 启动 ...

  6. 小程序构建npm出现没有找到node_modules

    以下转自:https://blog.csdn.net/zhangyabo_code/article/details/86162671 npm initnpm install --production ...

  7. mysql 5.5数据导入5.7 Failed - Error on Table user - 1067 - Invalid default value for 'CREATE_date'

    表结构是这样 DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (....省略了一些无关紧要的字段 `CREATE_DATE_` timestamp NO ...

  8. python 虚拟环境使用与管理(virtualenv)

    安装虚拟环境 pip install virtualenv 安装虚拟环境管理工具 pip install virtualenvwrapper-win 设置虚拟环境目录(虚拟环境存放位置) 默认创建的虚 ...

  9. 5. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    解决方案,见  https://www.jianshu.com/p/836d455663da

  10. vue每次修改刷新当前子组件

    刚入门vue,发现很多坑,对很多框架兼容性不太友好,比如layui等 每次删除相关信息,更新相关信息,不会主动刷新当前页面内容,只能手动刷新 第一步,我们在跟组件理由设置一个参数,用来判断是否需要刷新 ...