leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers
1 题目
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
接口: public int divide(int dividend, int divisor)
2 思路
题意
不用乘、除、mod
做一个除法运算。
解
直接用除数去一个一个加,直到被除数被超过的话,会超时。
解决办法:每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作。
复杂度: O(n)
3 代码
public int divide(int dividend, int divisor) {
// 处理负数
int xor = dividend ^ divisor;
int signal = xor >= 0 ? 1 : -1;
// 正常逻辑
long count = 0;
long num = Math.abs((long) dividend), div = Math.abs((long) divisor);
long tmp = div;
while (num >= tmp) {
long countTmp = 1;
while (num >= tmp) {
tmp = tmp << 1;
countTmp = countTmp << 1;
}
count += countTmp >> 1;
tmp = tmp >> 1;
num = num - tmp;
tmp = div;
}
// 处理溢出的特殊用例{-2147483648, -1}
long res = count * signal;
return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)res;
}
4 总结
数学思维,考智商。
leetcode面试准备:Divide Two Integers的更多相关文章
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】29. Divide Two Integers
题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
随机推荐
- Dom操作--全选反选
我们经常会在网站上遇到一些多选的情况,下面我就来说说使用Dom写全选反选的思路. 全选思路:首先,我们来分析一下知道,当我们点击"全选"复选框的时候,所有的复选框应该都被选中,那我 ...
- jsf taglib定义函数
创建文件 在文件中添加function标签 <function> <function-name>getFileContent</function-name> & ...
- oracle 定义临时表
创建Oracle 临时表,可以有两种类型的临时表: 会话级的临时表 事务级的临时表 . 1) 会话级的临时表因为这这个临时表中的数据和你的当前会话有关系, 当你当前SESSION不退出的情况下,临时表 ...
- struts2类型转换中的错误处理
由于类型转换过程中有可能出现原始参数无法转换为目标类型的错误,所以struts2提供了类型转换中的异常处理机制. 在struts2的默认配置文件struts-default.xml中有如下一段配置代码 ...
- Headfirst设计模式的C++实现——抽象工厂(Abstract Factory)
Dough.h #ifndef _DOUGH_H #define _DOUGH_H class Dough { }; #endif ThinCrustDough.h #ifndef _THIN_CRU ...
- Web Service 的服务端的引用
1.先说说服务端的引用 先写一个Web Service 的文件 上图 创建一个web 项目或者网站 然后添加新项 创建一个web服务 得到 下面的页面 然后运行起来 然后复制下地址 接下来创建另一 ...
- 《sed的流艺术之二》-linux命令五分钟系列之二十二
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- Yii 跨域设置
控制器设置: abstract class ControllerBase extends Controller { public function __construct($id, $module, ...
- SWD模式和JTAG模式
一.功能 SWD模式:仿真 下载 JTAG模式:仿真 下载 二.接口 1.J-link JTAG/SWD接口 2.开发板接口电路 ①SWD模式 4根线(包片机) ②JTAG模式 20脚JTAG(网络) ...
- SimpleMembership: The future of membership for ASP.NET
http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal- ...