29. Divide Two Integers (JAVA)
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)的更多相关文章
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 29. Divide Two Integers (INT; Overflow, Bit)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- [转][ActiveMQ]Apache.NMS.ActiveMQ 用法
下载 C# 组件:http://archive.apache.org/dist/activemq/apache-nms/1.7.0/ 使用说明:https://www.cnblogs.com/cjm1 ...
- Submine Text3格式化HTML/CSS/JS代码
Submine Text3格式化HTML/CSS/JS代码需要安装插件,步骤如下: 1.打开菜单--->首选项---->Package Control,输入 install package ...
- 研究并尝试改进Vyeshal
我想研究Vyeshal,这样就不可避免的要用到游戏中的语音.然而游戏内的语音文件是.bank类型的,如何转化为可听文件是个问题.
- 著名java博客
http://blog.csdn.net/net19880504/article/details/20807403
- (详细)华为荣耀8X JSN-AL00的usb调试模式在哪里开启的教程
经常我们使用Pc链接安卓手机的时候,如果手机没有开启usb开发者调试模式,Pc则没办法成功识别我们的手机,有时候,我们使用的一些功能比较强的的工具比如之前我们使用的一个工具引号精灵,老版本就需要开启u ...
- flutter 入口文件配置路由+加载页面
入口文件配置路由 1.路由信息 -- 加载页面 ,通常用于显示新的内容或者广告,加载完成之后进入主页面 -- 主页面 /app 2.配置页面 main.dart main.dart // main ...
- python类特列方法使用
class Rgc(object): def __new__(cls, *args, **kwargs): print('在类通过__new__方法实例化一个对象') return super(Rgc ...
- 为什么vue支持IE9以上的IE浏览器?
原因如下: 1.vue框架中核心的双向绑定原理是利用Object.defineProperty()方法实现的. 2.该方法第一个被实现是在IE8中,但是存在诸多限制:只能在DOM对象上使用这个方法,而 ...
- java内存问题排查及分析
最近了解了一下jdk对于jvm分析工具的使用,下面通过一个简单的列子介绍一下,以下内容部分来自其他帖子. 下面这段代码明显有问题(从网上抄的) import java.util.HashMap; im ...
- WPF DEV gridcontrol当前项的数据导出为mdb文件
/// <summary> /// 导出为mdb /// </summary> /// <param name="sender"></pa ...