题目

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

题解

这道题我自己没想出来。。。乘除取模都不让用。。那只有加减了。。。我参考的http://blog.csdn.net/perfect8886/article/details/23040143

代码如下:

 1      public int divide(int dividend, int divisor) {  
 2         if (dividend == 0 || divisor == 0) {  
 3             return 0;  
 4         }  
 5         boolean isNeg = (dividend > 0 && divisor < 0)  
 6                 || (dividend < 0 && divisor > 0);  
 7         long a = Math.abs((long) dividend);  
 8         long b = Math.abs((long) divisor);  
 9         if (b > a) {  
             return 0;  
         }  
   
         long sum = 0;  
         long pow = 0;  
         int result = 0;  
         while (a >= b) {  
             pow = 1;  
             sum = b;  
             while (sum + sum <= a) {  
                 sum += sum;  
                 pow += pow;  
             }  
             a -= sum;  
             result += pow;  
         }  
         return isNeg ? -result : result;  
     } 

Reference:

http://blog.csdn.net/perfect8886/article/details/23040143

Divide Two Integers leetcode java的更多相关文章

  1. 29. Divide Two Integers - LeetCode

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

  2. Divide Two Integers leetcode

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

  3. Divide Two Integers —— LeetCode

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

  4. Java for LeetCode 029 Divide Two Integers

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

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

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

  6. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

  7. LeetCode: Divide Two Integers 解题报告

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

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

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

  9. leetcode面试准备:Divide Two Integers

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

随机推荐

  1. 洛谷P3265 [JLOI2015]装备购买 [线性基]

    题目传送门 装备购买 格式难调,题面就不放了. 分析: 一句话,有$n$件物品,每件物品有$m$个属性和一个花费值,如果一个装备的属性值可以由其他装备的属性值改变系数后组合得到那就不买,求购买最多装备 ...

  2. C++ shared_ptr

    晕晕乎乎,其他的再补充 1.shared_ptr 主要是为了方便管理内存而存在的,C++程序中不会再出现new 和 delete,内存的分配和析构全部由shared_ptr进行管理 2.当程序中对某个 ...

  3. C# 非模式窗体show()和模式窗体showdialog()的区别

    对话框不是模式就是无模式的.模式对话框,在可以继续操作应用程序的其他部分之前,必须被关闭(隐藏或卸载).例如,如果一个对话框,在可以切换到其它窗 体或对话框之前要求先单击"确定"或 ...

  4. 解决在ubuntu环境下, sublime不能输入中文的问题

    sublime text很好用,但是ubuntu下不能输入中文,这是一个很大的问题.网上已经有很多方法,这里将我自己使用的方法记录总结一下 首先,将你的操作系统升级到最新版: sudo apt-get ...

  5. Java与GIS的联系

    Java与GIS的联系 地理信息系统是70年代初发展起来的一门新兴的边缘学科.    由于GIS在数据采集与输入.空间数据管理.地图提取.自动制图.数字地形分析.数据输出等方面具有强大而又独特的功能  ...

  6. Problem A: 象棋比赛

    Description 1月6日,教职工象棋协会在6号楼办了一次比赛,很多老师都参加了.比赛共进行了5轮,赢1局积3分,和了1分,输了0分,你能帮忙算一下各位老师的积分吗? Input 多组测试数据, ...

  7. 【2017多校训练08 1002】【HDOJ 6134】Battlestation Operational

    典型的数列反演题. 运用莫比乌斯反演的一个结论 $[n = 1] = \sum_{d | n} \mu(d)$,将表达式做如下转化: $$ ans = \sum_{i=1}^n \sum_{j=1}^ ...

  8. STL 优先队列详解

    优先队列是一个保证队列里元素单调的队列,我们可以利用它来维护一个线性结构的单调性. 一般的优先队列: 当然需要加头文件 #include <queue> priority_queue &l ...

  9. VC 操作 EXCEL---插入工作表(Insert.Sheet)方法

    看到的资料 http://bbs.csdn.net/topics/198565 自己总结一下 //插入到nIndex工作表之前 void InsertSheet(int nIndex) { sheet ...

  10. BZOJ 2756: [SCOI2012]奇怪的游戏 网络流/二分

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1594  Solved: 396[Submit][Stat ...