[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.
题目要求不使用乘除和取余的运算得到除法的结果,我们第一个想到的肯定是减法,除法的本质就是减法嘛,但是减法的时间复杂度很不友好,如果被除数是Integer.MAX_VALUE,除数是1,那就很麻烦了
在这个基础上我们想到了更高级一点的操作方法,位移运算,<<1表示把字节左移一位相当于*2,我们不断扩大除数divisor,直到它再扩大于被除数,这个时候我们需要缩小被除数,就用这个被除数减去当前的除数然后再重复之前的过程,就得到了我们想要的结果。这里需要注意的是测试样例存在integer.MAX_VALUE和-1的特殊情况,所以我们要使用long来帮助存储判断
class Solution {
public int divide(int dividend, int divisor) {
long m=Math.abs((long)dividend);
long n=Math.abs((long)divisor);
long res=0,tag=Integer.MAX_VALUE;
if(m<n) return 0;
while(m>=n){
long temp=n,count=1;
while(m>(temp<<1)){
temp<<=1;
count<<=1;
}
m-=temp;
res+=count;
}
if((dividend<0)^(divisor<0)) res=-res;
return (int)(res>tag ? tag:res);
}
}
[LeetCode]29. Divide Two Integers两数相除的更多相关文章
- [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, divisio ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 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 ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- Linux--CentOS7使用firewalld打开关闭防火墙与端口
1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...
- 洛谷 P1372 又是毕业季I
可能所有的数论题都是这样玄学.... 题目链接:https://www.luogu.org/problemnew/show/P1372 这道题通过暴力的枚举可以发现是不可做的(当然我也不会做) 然后就 ...
- apache的URL重写
apache的url重写 第一步:修改apache\conf目录下的的httpd.conf文件 1.加载apache的url重写模块 大概122行:LoadModule rewrite_module ...
- [Maven]Codehaus的Maven Repository地址
In ~/.m2/settings.xml you can update the URL to be used for specific repositories. For example: < ...
- MapReduce编写的正确姿势
先看一下目录结构 这里是job接口,负责参数的传递和定时的调用 下面的图是MR 程序相关的目录图片,其中MR的入口程序负责读取数据,并指定对应的Map.Reduce程序. 程序的流程 首先简单的说一下 ...
- python学习之路---day21--模块和栈
模块和栈 一:计数模块collections 基础版本: s="qwewsfdfjiehrfqweqweqwqewq" dic={} for el in s: dic[el]=di ...
- UVA - 11996 可持久化Treap 维护Hash Ver.2
这回总算是过了.. 4600ms+,服务器抖一抖又没了 对于极端卡时间的情况还是考虑屈服于Splay吧 #include<iostream> #include<algorithm&g ...
- [转] 一文看懂npm、yarn、pnpm之间的区别
[From] http://geek.csdn.net/news/detail/197339 原文:Understanding differences between npm, yarn and pn ...
- UML-1-面向对象分析和设计
1.关键词: OOA:Object-Oriented Analysis.面向对象分析.抽取对象或概念,如:航班系统包含 飞机(Plane).航班(Flight)等概念. OOD:Object-Orie ...
- 用jenkins编译WPF程序并传输到服务器
环境准备: 1.ide visual studio2017 Enterprise,Ide可以https://visualstudio.microsoft.com/zh-hans/downloads ...