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:

  1. Input: dividend = 10, divisor = 3
  2. Output: 3

Example 2:

  1. Input: dividend = 7, divisor = -3
  2. 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取绝对值后会溢出
  1. class Solution {
  2. public int divide(int dividend, int divisor) {
  3. //handle overflow
  4. int res = 0;
  5. if(dividend==Integer.MIN_VALUE)
  6. {
  7. if(divisor==-1)
  8. return Integer.MAX_VALUE;
  9. res = 1;
  10. dividend += Math.abs(divisor);
  11. }
  12. if(divisor==Integer.MIN_VALUE)
  13. return res;
  14.  
  15. //handle negative
  16. Boolean isNeg = false;
  17. if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) isNeg = true;
  18. dividend = Math.abs(dividend);
  19. divisor = Math.abs(divisor);
  20.  
  21. int cnt = 0; //记录divisor左移的次数
  22. //找到商的最高比特位
  23. while(divisor <= (dividend >> 1)){ //除数与被除数/2相比,为了保证除<被除数,且防止溢出
  24. divisor <<= 1;
  25. cnt++;
  26. }
  27.  
  28. //从最高比特位开始求出商的每一位
  29. while(cnt >= 0){
  30. if(dividend >= divisor){ //dividend > divisor说明当前比特位为1,否则为0
  31. dividend -= divisor;
  32. res += 1 << cnt;
  33. }
  34. divisor >>= 1;
  35. cnt--;
  36. }
  37.  
  38. return isNeg?-res:res;
  39. }
  40. }

通过二进制位移完成乘除,每次左移一位,相当于十进制中*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. sql 查询结果转百分比

    select convert(varchar,convert(decimal(10,2),迟到人次*1.0/在校生人数*100))+'%'

  2. MYSQL登录函数(第3版本)

    已经改进 CREATE DEFINER=`root`@`%` FUNCTION `uc_session_login`( `reqjson` JSON, `srvjson` JSON ) RETURNS ...

  3. linux下怎么清理缓存

    free -m 命令可以查看内存使用情况 sysctl 命令可以临时改变某个系统参数  如:sysctl -w net.ipv4.ip_forward=1 是将forware参数临时改为1 当 ser ...

  4. mysql error(2003) 10060的再解决

    前段时间在window虚拟机上处理过这样的问题 现在在linux上也遇到了这样的问题一项一项的排查 1.网络问题,ping的通 但是telnet (ip)  (端口号)失败,telnet(ip)都失败 ...

  5. ABAP笔记

    ABAP程序开发,经常会遇到报表开发需求.使用ABAP的Report类型程序开发报表十分便利,用很少的代码就可以快速开发出一个报表.这种报表需求,抛开各种细枝末节,都可以归结为“三步走”:1.选择屏幕 ...

  6. Target JRE version (1.7.0_79) does not match project JDK version (java version "1.8.0_171"), will use sources from JDK: 1.7

    IDEA不会自动匹配系统的JDK环境.如果在IDEA里面没有配置JDK,那么运行程序时就会报错 之前碰到这个问题卡了一下 顺手记录一下 出现此错误说明IDE中配置的jdk版本和你使用的jdk版本不一致 ...

  7. 封装poi导出篇

    前言 先写的导入,以为导出会很简单,没想到导出的东西也不少,基于常用的几种样式和校验写了一个简单的导出,包括时间,数字,文字长度,下拉框,提示框校验,基础样式包括字体,字体颜色,背景颜色等功能,可以使 ...

  8. springboot学习二:配置文件配置

    springboot默认读取application*.properties #######spring配置####### spring.profiles.active=dev //引入开发配置文件 a ...

  9. c语言实现:三子棋

    问题描述:两个游戏者在3*3棋盘里轮流作标记,如果一个人在行,列或者两个对角线可以作三个标记,则为获胜. 我们首先得打印菜单供玩家选择(可以选择玩游戏或者退出游戏) void menu() { pri ...

  10. C++builder Tokyo 调用com 不正确的变量类型

    C++builder Tokyo 调用com 不正确的变量类型 tt.OleFunction("interface_call","MS01",&erro ...