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 ...
随机推荐
- SSM框架-初学Mybatis框架
SSM(Spring+SpringMVC+Mybatis)是目前项目开发比较流行的一套组合框架,而Mybatis是负责数据库操作的那部分框架,具体 我也说不上来 传统的JDBC操作比较冗长而繁琐,而用 ...
- windows下共享式服务开发
参考: https://blog.csdn.net/dongyewolong/article/details/8164873 https://blog.csdn.net/qwertyupoiuytr/ ...
- Python基础之条件语句和循环
条件语句 Python中的条件语句分为 if ...else . if ...elif...else 以及if ...else 的嵌套使用: username = input('请输入您的用户名:' ...
- python守护进程
1.守护进程 [1]使用runner这个模块直接创建守护进程,非常方便. [2]运行方法:python xxx.py start|stop|restart [3]调用python xxx.py sto ...
- linux 安装中文支持
下载 fonts-chinese-3.02-12.el5.noarch.rpm fonts-ISO8859-2-75dpi-1.0-17.1.noarch.rpm 安装各种提示的依赖 安装 chkf ...
- PADS导入DXF板框,不能将开放的2D线转换成闭合的板框错误
刚开始学会用PADS,学习的时候都是在PADS里手绘一个板框的.然后实际项目中,都是需要导入结构DXF板框文件,第一次导入就发现了问题. 第一次导入DXF后,需要将DXF转换为板框,但提示 “不能将开 ...
- 试用 Angular v6 的 Ivy compiler
“Ivy” 是 Angular v6 的新一代渲染器.从 v6.0.0-beta.1 开始,Ivy 已经作为体验 API 发布. 作为下一代的 Angular 的视图引擎,重点在于彻底缩减代码尺寸并增 ...
- 2018-2019-2 20175328 《Java程序设计》第八周学习总结
2018-2019-2 20175328 <Java程序设计>第八周学习总结 主要内容 泛型 泛型推出的主要目的是可以建立具有类型安全的集合框架,如链表.散列映射等数据结构. 1.泛型类声 ...
- this 的指向
使用 JavaScript 开发的时候,很多开发者多多少少会被 this 的指向搞蒙圈,但是实际上,关于 this 的指向,记住最核心的一句话:哪个对象调用函数,函数里面的this指向哪个对象. 下面 ...
- 一、Python入门
一.语法特点: 注释规则: 单行注释:“#”作为单行注释符号(从“#”开始到换行都为注释):Alt+F3/4快捷添加/取消注释 多行注释:宝行一对三引号('''…''')或(""& ...