[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.
题意:
不准乘除,不准膜。
Solution1:用加减和位运算。
乘法可以通过不断加法来获得
故,除法可以通过不断减法来获得
碎碎念:
数学渣对这种没有算法含量,又要大量数学sense的题,好抓狂。
发挥文科生优势。背诵吧!!
code
class Solution {
public int divide(int dividend, int divisor) {
// corner case
if(dividend == 0) return 0;
// 当 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, division ...
- [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 ...
随机推荐
- mysql下载以及安装
因为xampp怎么都连接不上mysql,我感觉有可能是因为装mysql的时候试了很多次才安装成功,之前的mysql没有卸载干净造成的,今天把mysql卸载干净,又重新安装配置环境,但是还是连接不上,然 ...
- pxe+Kickstart自动装机补充知识点
1.vmlinuzvmlinuz是可引导的.压缩的内核.“vm”代表“Virtual Memory”.Linux 支持虚拟内存,不像老的操作系统比如DOS有640KB内存的限制.Linux能够使用硬盘 ...
- Docker进入容器后使用ifconfig等命令“command not found”解决办法
当进入一个容器后,使用ifconfig.ip addr等命令时,出现如下“command not found”: 解决办法: yum update yum -y install n ...
- utf-8mb4和排序规则
MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode. 最新的 UTF-8 规范只使用一到四个字节,最大能编码21位, ...
- Java12配置
配置环境变量: 之前的JAVA_HOME和CLASSPATH已经都不要了.只要配置jdk的bin到Path里: C:\Program Files\Java\jdk-12\bin
- centos7如何使用yum命令
参照https://www.cnblogs.com/zhongguiyao/p/9029922.html 参照https://blog.csdn.net/shuaigexiaobo/article/d ...
- WPF Chart
https://quant.stackexchange.com/questions/7065/how-to-create-charts-in-wpf-finance-applications
- TypeScript 模块系统
https://www.cnblogs.com/niklai/p/5808789.html
- JAVA 异常类型结构分析
JAVA 异常类型结构分析 Throwable 是所有异常类型的基类,Throwable 下一层分为两个分支,Error 和 Exception. Error 和 Exception Error Er ...
- Chapter 5 数组:为什么很多编程语言种数组都是从0开始编号?
如何实现随机访问? 线性表:数组,队列,链表,栈 非线性表:树,图 总结:数组用一块连续的内存空间,来存储相同类型的一组数据,最大的特点就是支持随机访问,但插入,删除操作也因此变得比较低效,平均情况时 ...