[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 ...
随机推荐
- python 标准日志模块loging 及日志系统实例
本文出处:https://www.cnblogs.com/goodhacker/p/3355660.html#undefined python的标准库里的日志系统从Python2.3开始支持.只要im ...
- Scrum立会报告+燃尽图(Beta阶段第三次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2385 项目地址:https://coding.net/u/wuyy694 ...
- 咱们的team1序章
之前都参加了好多组织,这是第一次参加变成组织.首先要介绍团队名称了,为什么叫“咱们的team”呢,因为,我们需要每个人都认真的参与进来,只有每个人都十分投入地参与进来,这个team才能称之为一个tea ...
- Java 数组转字符
public static String toString(int[] arr){ String temp = ""; for(int i = 0;i<arr.length; ...
- EasyUI中DataGrid构建复合表头
在使用easyui的DataGrid控件时,构建复合表头就显得非常简单了.只需要在使用columns属性时通过数组的方式编写列名即可.如我们需要构建成一个如下的表头: Columns的代码如下: co ...
- (转)Elasticsearch search-guard 插件部署
我之前写了ELK+shield的部署文档,由于shield是商业收费的,很多人都推崇开源项目search-guard来做ELK的安全组件,准确来说是elasticsearch的安全组件.search- ...
- libcurl底层调用逻辑
libcurl就不多介绍了,一个支持HTTP,FTP,SMTP等协议的网络库 只涉及multi部分,easy部分就不提了. 两个线程,一个负责添加HTTP请求,另一个轮询,负责处理每一个请求 Thre ...
- 第207天:HTTP协议头字段详解大全
本篇重点介绍一下HTTP常用的Header HTTP Header非常之多,很少有人能完全分清这些Header到底是干什么的.鉴于RFC文件规范艰深晦涩难懂,本文对协议规范中列出的HTTP Heade ...
- 【.Net+数据库】sqlserver的四种分页方式
第一种:ROW_NUMBER() OVER()方式 select * from ( select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId ...
- Java内存区域介绍
Java虚拟机把内存划分成几个区域,每个区域都有各自的职责.下面将逐一分析每个区域. 有助于我们了解,每个方法,变量,对象等都去哪儿了! 程序计数器: 它占用一块很小的内存空间,可以看作是当前线程所执 ...