[leetcode]29. Divide Two Integers 两整数相除
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.
 
题目
不准乘除,不准膜。
思路
那剩下的还能用加减、位运算
代码
// Divide Two Integers
// 时间复杂度O(logn),空间复杂度O(1)
public class Solution {
public int divide(int dividend, int divisor) {
if(dividend == 0) return 0;
if (divisor == 0) return Integer.MAX_VALUE; // 当 dividend = INT_MIN,divisor = -1时,结果会溢出
if (dividend == Integer.MIN_VALUE) {
if (divisor == -1) return Integer.MAX_VALUE;
else if (divisor < 0)
return 1 + divide(dividend - divisor, divisor);
else
return - 1 + divide((dividend + divisor), divisor);
}
if(divisor == Integer.MIN_VALUE){
return dividend == divisor ? 1 : 0;
} int a = dividend > 0 ? dividend : -dividend;
int b = divisor > 0 ? divisor : -divisor; int result = 0;
while (a >= b) {
int c = b;
for (int i = 0; a >= c;) {
a -= c;
result += 1 << i;
if (c < Integer.MAX_VALUE / 2) { // prevent overflow
++i;
c <<= 1;
}
}
} return ((dividend^divisor) >> 31) != 0 ? (-result) : (result);
}
}
[leetcode]29. Divide Two Integers 两整数相除的更多相关文章
- [leetcode]29. Divide Two Integers两整数相除
		
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
 - [LeetCode] 29. Divide Two Integers 两数相除
		
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
 - [LeetCode]29. Divide Two Integers两数相除
		
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
 - 【LeetCode每天一题】Divide Two Integers(两整数相除)
		
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
 - [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
		
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
 - [LeetCode] Divide Two Integers 两数相除
		
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
 - [LeetCode] 29. Divide Two Integers ☆☆
		
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
 - 029 Divide Two Integers 两数相除
		
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
 - [LintCode] Divide Two Integers 两数相除
		
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
 
随机推荐
- HTC Vive开之unity3d开发
			
常用的几款插件 Steam VR, SteamVR Unity Toolkit 配置要求:显卡不低于GTX960性能的主机 一.引入手柄交互 1.通过Asset Store导入SteamVR Plu ...
 - kudu架构(转)
			
特点: High availability(高可用性).Tablet server 和 Master 使用 Raft Consensus Algorithm 来保证节点的高可用,确保只要有一半以上 ...
 - Glusterfs3.3.1DHT(hash分布)源代码分析
			
https://my.oschina.net/uvwxyz/blog/182224 1.DHT简介 GlusterFS使用算法进行数据定位,集群中的任何服务器和客户端只需根据路径和文件名就可以对数据进 ...
 - java中key-value数据有重复KEY如何存储
			
http://www.iteye.com/problems/87219 Map<Key, List<Value>>, 这个好 师兄厉害,给介绍了个神器:guava
 - mysql更新(七) MySQl创建用户和授权
			
14-补充内容:MySQl创建用户和授权 权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作 ...
 - JS监听浏览器事件
			
Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...
 - django中使用Ajax
			
内容: 1.Ajax原理与基本使用 2.Ajax发送get请求 3.Ajax发送post请求 4.Ajax上传文件 5.Ajax设置csrf_token 6.django序列化 参考:https:// ...
 - 微信通过openID发送消息/后台post、get提交并接收数据
			
控制器:下面是post发送消息(微信不支持从前台发送数据,之前试过,报错,需要跨域,跨域的问题解决后还不行,最后发现之后后端提交 WXApi类: #region 验证Token是否过期 /// < ...
 - sqoop1 与sqoop2的对比
			
Sqoop是一款开源的工具,主要用于在Hadoop和传统的数据库(mysql.postgresql等)进行数据的传递,可以将一个关系型数据库(例如:MySQL.Oracle.Postgres等)中的数 ...
 - jsfl 发布保存关闭
			
fl.getDocumentDOM().publish(); fl.getDocumentDOM().save(); fl.getDocumentDOM().close();