Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

题目大意:不用乘除取模运算计算两个数的除。

解题思路:只能用位运算了,当被除数大于除数,除数左移1位、2位……直到得到最大的然后用被除数减去它,将因数加到res上,这里有点二分的意思,依次循环往复,这里我把两个数都设为负数,因为0x80000000是最小的负数,它没有对应的最大正数。结果的正负由两个的符号决定,记录一个negFlag。代码感觉写的不够优雅,但是目前也就能写成这样了。

    public int divide(int dividend, int divisor) {
//overflow
if (divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)) {
return 0x7fffffff;
}
if (dividend == 0) {
return 0;
}
int res = 0;
boolean negFlag = (dividend ^ divisor) < 0;
dividend = dividend < 0 ? dividend : -dividend;
divisor = divisor < 0 ? divisor : -divisor;
while (dividend <= divisor) {
int offset = 0;
int tmp = divisor;
while (dividend <= tmp) {
tmp = divisor << offset;
offset++;
if (tmp < (Integer.MIN_VALUE >> 1)) {
break;
}
}
offset -= 2;
tmp >>= 1;
dividend -= tmp;
res += (1 << offset);
if (offset == -1) {
res = 1;
} else if (offset == -2) {
res = 0;
}
}
return negFlag ? -res : res;
}

Divide Two Integers —— LeetCode的更多相关文章

  1. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  2. Divide Two Integers leetcode java

    题目: Divide two integers without using multiplication, division and mod operator. 题解: 这道题我自己没想出来...乘除 ...

  3. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  4. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  6. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  7. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  8. LeetCode: Divide Two Integers 解题报告

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...

  9. 【Leetcode】【Medium】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

随机推荐

  1. 10.6 noip模拟试题

    更正:第三组:不存在相同的字符|str|=26,26<=n<=100 60 /* 呵呵哒~这题 正解还在研究.... 因为没有题解只有个std还在看 不过乱搞一下可以70(数据好像有问题只 ...

  2. Extjs ——radiogroup子元素宽度调整

    配置项 类型 说明 allowBlank Boolean 设置是否必须选择至少一项,true表示可以不选,false表示不能为空至少选一项,默认为true blankText String 当allo ...

  3. 【css面试题】三个DIV要求水平对齐,左右两个DIV宽度固定为100px,中间那个DIV充满剩余的宽度(至少2种方法)

    这是我在一家公司面试时遇到的问题,当时没有答上来!! 所以看到的小伙伴一定要注意了!! 变化浏览器宽度可看到效果: 左 右 中 然后我们来看看代码: 第一种方法:(浮动) <style type ...

  4. SQL SERVER将指定表中的指定字段按照(,)逗号分隔

    不开心呀,早知道不跳了,一跳跳坑里来了. 使用方式: DECLARE @ConsigneeAddressId INT; SET @ConsigneeAddressId = 1; SELECT  * F ...

  5. mysql出现的错误

    (一)ERROR 1005 (HY000): Can't create table '.\day19\user_role.frm' (errno: 121) 今天遇到的这个问题是因为创建了五张表,其中 ...

  6. .net RAW(16)与GUID互相转换

    .net 1.raw转guidnew guid(byte[] id);2.guid转rawGuid result;string ids = BitConverter.ToString(result.T ...

  7. 如何管理你的 Javascript 代码

    今天不聊技术的问题,咱们来聊聊在前端开发中如何管理好自己的 Javascript 代码.首先,咱们先来说说一般都有哪些管理方式?我相信  seajs . requirejs  对于前端开发者而言都不陌 ...

  8. 【转】 iOS KVO KVC

    原文: http://www.cocoachina.com/industry/20140224/7866.html Key Value Coding Key Value Coding是cocoa的一个 ...

  9. 前端,移动开发者,UI须懂: 不同设备的之间的尺寸

    在开发前端,移动APP,以及设计UI的时候,我们经常会去搜索不同设备之间的尺寸,来开始自己的工作,以保证显示效果达到更好,这里收集了现在常用的设备. 设备更新速度快,有些没罗列的,大家可以谷歌或者百度 ...

  10. PHP 用户注册与登录

    网站用户注册与登录是很常用的一个功能,本节教材就以此来演示一下 PHP 中如何开发用户注册与登录模块. 本节需要用到的重点 PHP 基础知识: PHP 中预定义 $_POST 和 $_GET 全局变量 ...