题目

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

题解

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

代码如下:

  1.  1      public int divide(int dividend, int divisor) {  
  2.  2         if (dividend == 0 || divisor == 0) {  
  3.  3             return 0;  
  4.  4         }  
  5.  5         boolean isNeg = (dividend > 0 && divisor < 0)  
  6.  6                 || (dividend < 0 && divisor > 0);  
  7.  7         long a = Math.abs((long) dividend);  
  8.  8         long b = Math.abs((long) divisor);  
  9.  9         if (> a) {  
  10.              return 0;  
  11.          }  
  12.    
  13.          long sum = 0;  
  14.          long pow = 0;  
  15.          int result = 0;  
  16.          while (>= b) {  
  17.              pow = 1;  
  18.              sum = b;  
  19.              while (sum + sum <= a) {  
  20.                  sum += sum;  
  21.                  pow += pow;  
  22.              }  
  23.              a -= sum;  
  24.              result += pow;  
  25.          }  
  26.          return isNeg ? -result : result;  
  27.      } 

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. Java设计模式 - 持续更新

    注意,此博客来源于我的 OneNote 笔记本 因此属于图片形式进行展示,这意味着你可以: 不经过我的同意进行保存 不经过我的同意进行发布 我仍然希望搬运时留一个网址指明来处:我的博客园 多谢!以下是 ...

  2. Laravel框架初学一路由(路由参数)

    必要参数 有时需要在路由中捕获到URI的一些参数.比如,需要捕获URI中的用户id,可以这样来定义路由 Route::get("user/{id}", function ($id) ...

  3. 文件哈希审计工具md5deep/hashdeep

    文件哈希审计工具md5deep/hashdeep   在数据取证中,通常需要验证文件的哈希值,以判断文件是否已知好文件,或者文件是否被修改过.Kali Linux提供专用工具hashdeep.该工具的 ...

  4. JMS开发指南

    1.JMS消息的异步与同步接收 消息的异步接收: 异步接收是指当消息到达时,主动通知客户端,即当消息到达时转发到客户端.JMS客户端可以通过注册一个实现MessageListener接口的对象到Mes ...

  5. 【51Nod 1222】最小公倍数计数

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1222 求\([a,b]\)中的个数转化为求\([1,b]\)中的个数减去 ...

  6. hdu 4118 dfs

    题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值.不能重复 这题的方法很有看点啊,标记为巩固题 Sample Input 1 4 1 2 3 2 3 2 ...

  7. 4、Redis中对List类型的操作命令

    写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- ------------ ...

  8. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力

    C. Bear and Colors 题目连接: http://www.codeforces.com/contest/673/problem/C Description Bear Limak has ...

  9. 原生JS实现一个简单的前端路由(原理)

    说一下前端路由实现的简要原理,以 hash 形式(也可以使用 History API 来处理)为例, 当 url 的 hash 发生变化时,触发 hashchange 注册的回调,回调中去进行不同的操 ...

  10. dea工具debug断点红色变成灰色

    没事别瞎点,禁用了断点当然不走了