题目

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. 在控制台连接oracle

    Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\lijt>sqlplus ...

  2. JAVAEE——SSH项目实战05:用户注册、登陆校验拦截器、员工拜访客户功能和MD5加密

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7170519.html 一.用户注册   显示错误信息到页面上的另一种方法: public ...

  3. TPS和QPS是什么,他们的区别是什么

    一.TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数.TPS包括一条消息入和一条消息出,加上一次用户数据库访问.(业务TPS = CAPS × ...

  4. Xshell6和Xftp下载地址,rzsz的使用

    官方下载地址:https://cdn.netsarang.net/98f59c09/Xshell-6.0.0076r_beta.exe https://cdn.netsarang.net/98f59c ...

  5. 洛谷.4238.[模板]多项式求逆(NTT)

    题目链接 设多项式\(f(x)\)在模\(x^n\)下的逆元为\(g(x)\) \[f(x)g(x)\equiv 1\ (mod\ x^n)\] \[f(x)g(x)-1\equiv 0\ (mod\ ...

  6. hdu 4676 Sum Of Gcd 莫队+phi反演

    Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...

  7. hdu 1394 Minimum Inversion Number 逆序数/树状数组

    Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...

  8. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  9. ASP.NET 构建高性能网站 第2篇

    前言:在对ASP.NET网站进行优化的时候,往往不是只是懂得ASP.NET就足够了的. 在优化的过程中,一般先是找出问题可能存在的地方,然后证明找出的问题就是要解决的问题,确认之后,在进行一些措施.系 ...

  10. spring---aop(7)---Spring AOP中expose-proxy介绍

    写在前面 expose-proxy.为是否暴露当前代理对象为ThreadLocal模式. SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法(后续讲 ...