[LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
解法:
这道题让我们求两数相除,而且规定我们不能用乘法,除法和取余操作。
采用位运算中的移位运算,左移一位相当于乘2,右移一位相当于除以2。假设求 a / b,将b左移n位后大于a,则结果 res += 1 << (n - 1),将a更新 (a -= b << (n - 1)) 后进行同样操作,直到 a < b。
public class Solution {
public int divide(int dividend, int divisor) {
if (divisor == 0) {
return Integer.MAX_VALUE;
}
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean isNeg = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
long left = Math.abs((long)dividend);
long right = Math.abs((long)divisor);
int result = 0;
while (left >= right) {
int times = 1;
while ((right << times) <= left) {
times++;
}
left -= (right << (times - 1));
result += (1 << (times - 1));
}
return isNeg ? -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 ...
- 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, 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 ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
随机推荐
- sip鉴权认证算法详解及python加密
1. 认证和加密 认证(Authorization)的作用在于表明自己是谁,即向别人证明自己是谁.而相关的概念是MD5,用于认证安全.注意MD5仅仅是个hash函数而已,并不是用于加密.因为ha ...
- Scrum立会报告+燃尽图(十一月二十三日总第三十一次):界面修改及新页面添加
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2410 项目地址:https://git.coding.net/zhang ...
- Java:有关自定数组的学习
Java:有关==自定数组==的学习 在 ==<Java程序设计与数据结构教程>== 里我在==P212~P213==页看到一个GradeRange的程序,它用的数组是自定设定的Grade ...
- HTML5 <meta> 标签属性,所有meta用法
基本标签 声明文档使用的字符编码:<meta charset="utf-8" /> 声明文档的兼容模式:<meta http-equiv="X-UA-C ...
- 使用字符界面 qemu-kvm 创建虚拟机
qemu-kvm的基本用法:指定系统类型,CPU运行模式,NUMA(Non Uniform Memory Access Architecture), 软驱设备,光驱设备,硬件设备 # 查看qemu ...
- 爬虫学习之-python插入mysql报错
异常:'latin-1' codec can't encode characters in position 62-66: ordinal not in range(256) 用Python通过pym ...
- java 基础 --Collection(Set)
注意: 如果hashSet存储自定义对象,一定要重写hashCode()&&equals() 如果TreeSet存储自定义对象,让元素所属的类实现自然排序接口Comparable,并重 ...
- LoadRunner脚本参数化常见错误
错误代码:Error:missing newline in d:\loadrunner\username.dat 错误原因:场景设置不合理,参数数量不够,或者参数化文件有问题. 1)如果参数化文件反复 ...
- 【.Net】HttpClient 的使用
class Program { public static HttpClient Client; static HttpResponseMessage response; static void Ma ...
- Moya/RxSwift/ObjectMapper/Alamofire开发
废话不多说直接上代码 // // MoyaNetWorking.swift // GreenAir // // Created by BruceAlbert on 2017/9/18. // Copy ...