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. 怎么用js代码改变单选框的选中状态

    今天突然有一个需求要用到,使用js代码改变单选框的选中状态.当时想也不想直接 function doGender(gender) { if (gender == "男") { ge ...

  2. Use AUTO Mode with FOR XML (SQL Server 2012) Multiple Table

    SELECT Cust.CustomerID, OrderHeader.CustomerID, OrderHeader.SalesOrderID, OrderHeader.Status FROM Sa ...

  3. linux x64下编译libjpeg,libpng,zlib

    libJpeg编译: 下载libjpeg源码:http://www.ijg.org/,下载jpegsrc.v9a.tar.gz 解压源码,命令:tar -zxvf jpegsrc.v9a,源码文件夹为 ...

  4. 时间处理总结(三)javascript与WCF

    1.WCF提交时间时,若需接受DateTime需转换为"\/Date(928120800000+0800)\/"这种格式 var DateToJson = function (js ...

  5. ios tableview 上加 textfiled

    ios tableview 上加 textfiled 首先附上我项目中用曾经用到的几张图  并说明一下我的用法: 图1: 图2: 图3: 心在你我说一下  我当初的实现 方法 ,希望能给你们一些  启 ...

  6. CustomEditor的文件要放在Assets/Editor目录下

    using UnityEditor; using UnityEngine; [CustomEditor(typeof(test))] public class testEditor : Editor ...

  7. Library string type(2)——关于String的操作

    关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...

  8. Windows下ANSI、Unicode、UTF8字符编码转换

    主意:输入字符串必须是以'\0'结尾,如果输入字符串没有以'\0'结尾,请手动设置,否则转换会有错误. unsigned int EncodeUtil::AnsiToUcs2( char* pAnsi ...

  9. ajax跨域访问 webservice

    前端代码 $.ajax({ type: "POST", url: "http://localhost:9767/WebService1.asmx/HelloWorld?j ...

  10. erlang 里的if 和 case

    case Expression of Pattern1 [when Guard1] -> Expr_seq1; Pattern2 [when Guard2] -> Expr_seq2; … ...