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 ...
随机推荐
- 转HDMI
HDMI协议解析 2017年06月18日 14:19:27 阅读数:2987 转载请标明出处floater的csdn blog,http://blog.csdn.net/flaoter 本文从软件工程 ...
- linux git 保存账号密码
vi .git/config [credential] helper = store git pull 输入用户名.密码自动保存
- Memcache,redis,rabbitMQ,SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- C#将unix时间戳转换成.net的DateTime类型的代码
下面的内容是关于C#将unix时间戳转换成.net的DateTime类型的内容. DateTime epoch = new DateTime(1970,1,1,0,0,0,0, DateTimeKin ...
- Android给控件添加默认点击效果
Android控件点击效果 Android中Button控件是有点击效果的,但是像TextView.ImageView.各种Layout是没有点击效果的,给TextView设置点击事件后,加个点击效果 ...
- Python3实现自动点赞抖音小姐姐
什么是抖音 抖音是2016年9月上线的一款音乐创意短视频社交软件,是一个专注年轻人的15秒音乐短视频社区.用户可以通过这款软件选择歌曲,拍摄15秒的音乐短视频,形成自己的作品. 效果 抖音经常能刷到很 ...
- 【安全测试自学】初探web安全处测试(二)
自学资料: 安全测试专家成长系列之-初探Web安全2.mp4 XSS攻击 案例7:IPhone5 XSS盲打酷狗+后台SQL注射 CSRF即跨站请求伪造攻击: 举例: 上传漏洞: 危害: 文件包含漏洞 ...
- openssl error while loading serial number
unable to load number from D:/Program Files/OpenSSL-Win64/bin/demoCA/serialerror while loading seria ...
- mac环境破解navicat premium 12.1
1. 下载破解工具 https://github.com/DoubleLabyrinth/navicat-keygen/tree/mac 其中,navicat-keygen为破解器:navicat-p ...
- 一文读懂PRBS定义、生成办法、作用
对于眼图测试.误码率和抖动容限测试,最常用的测试码是PRBS,主要有PRBS7.PRBS15.PRBS23和PRBS31.本文主要解释了PRBS的定义,生成方法以及简单应用. PRBS定义 二进制序列 ...