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的更多相关文章

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

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

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

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

  3. LeetCode OJ:Divide Two Integers(两数相除)

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

  4. 【LeetCode】029. Divide Two Integers

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

  5. 【LeetCode】29. Divide Two Integers

    题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...

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

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

  7. LeetCode: Divide Two Integers 解题报告

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

  8. 【Leetcode】【Medium】Divide Two Integers

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

  9. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

随机推荐

  1. Apache 80 端口被占用无法重启解决办法

    原文出处 Apache 80 端口被占用无法重启解决办法 www.111cn.net 编辑:tiger 来源:转载使用WEB服务器的朋友都知道80端口是一个用来对外让用户访问的一个端口了,像apach ...

  2. 如何在已安装vs2010的基础上安装sql2008

    以前老受到别人写的这类东西的帮助,所以这次决定自己试下,第一次发这种,写得不好莫怪.       涉略sql2008一个多星期了.怎么说呢?Transact-SQL的编程虽然不如C++,java等高级 ...

  3. eclipse修改SVN下载的项目“>”变成“*”

    遇到过这样的问题,可能是因为下载的eclipse 的版本国过于高的原因. 一张图说明就行了. 将红色的地方打上勾就行了.

  4. linux字符过滤

    1  案例一:取eth0的IP地址 方法一:通过cut方法过滤 [root@baiguin ~]# ifconfig eth0|grep "inet addr:"|cut -d & ...

  5. 【转帖】客户端通过 HTTP 请求和响应 的 Header 信息总结

    请求Header原帖地址:http://technique-digest.iteye.com/blog/1174581 响应Header原帖地址:http://blog.pfan.cn/hurongl ...

  6. MinGW-notepad++开发c/c++程序

    下载MinGW 点击下载 安装好后运行 最后点击左上角的 Installation,开始安装 1.编译: g++ -o a.exe a.cpp gcc -o hello.exe hello.c 2.运 ...

  7. 06_WebService与Socket的区别

    [区别] 区别1. Socket是基于TCP/IP的传输层协议. WebService是基于HTTP协议传输数据的,HTTP是基于TCP的应用层协议. 区别2. WebService采用了基于HTTP ...

  8. Nuage SDN

    Nuage推出纯软件解决方案虚拟化业务平台(VSP)由三部分组成:虚拟化业务目录(VSD).虚拟化业务控制器(VSC)和虚拟路由与交换(VRS). VSD是业务/IT策略引擎,可提供业务模板与分析,每 ...

  9. Linux apt-get error

    csh@csh-laptop:~/ejabberd-15.03$ sudo apt-get install mysqlReading package lists... DoneBuilding dep ...

  10. tp_shop解读1

    由于想弄一个商城,因此研究了一下tp_shop,这个据说能完成几乎所有的功能. 考虑到原有的例子过于复杂,因此把所有相关的数据都删除了,结果上来就出错了,查了两天,大致弄清楚了状况. 关于错误的原因 ...