题目

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. [HDU - 5408] CRB and Farm

    题意: 给出一个由n个点组成的凸包,以及凸包内的k个点,问是否能够在凸包上选择最多2k个点构造一个新的 凸包,使得新的凸包覆盖原来的k个点. 要求2k个点覆盖原本的k个点,只要对原k个点构造凸包,然后 ...

  2. Python 入门之基本数据类型

    为什么我要学习Python这门语言呢?其实很简单,我想拓展技术面的同时,尝试更多的方向,可能最后会不了了之,谁知道呢?有可能的话,我会向爬虫和数据分析这个方向走.所以也就开始了我的Python学习之旅 ...

  3. 美团DB数据同步到数据仓库的架构与实践

    背景 在数据仓库建模中,未经任何加工处理的原始业务层数据,我们称之为ODS(Operational Data Store)数据.在互联网企业中,常见的ODS数据有业务日志数据(Log)和业务DB数据( ...

  4. gpfs 内核错误

    centos7.3安装旧的GPFS引发内核错误 没有关闭之前是可以查看到smap cat /proc/cpuinfo | grep smap 系统层关闭,也可以正常使用gpfs grubby --up ...

  5. Django一些开发经验

    总结一些 Django 开发的小经验.先说一些最最基础的吧. 使用 virtualenv 隔离开发环境 使用 pip 管理项目依赖,主要就是一个小技巧,使用 pip freeze > requi ...

  6. AtCoder Grand Contest 019 F-yes or no

    AtCoder Grand Contest 019 F-yes or no 解题思路: 考虑一个贪心策略,假设当前还有 \(x\) 道 \(\text{yes}\) 和 \(y\) 道 \(\text ...

  7. 【10.26校内测试】【状压?DP】【最小生成树?搜索?】

    Solution 据说正解DP30行??? 然后写了100行的状压DP?? 疯狂特判,一算极限时间复杂度过不了aaa!! 然而还是过了....QAQ 所以我定的状态是待转移的位置的前三位,用6位二进制 ...

  8. [Cocos2dx] CCCamera照相机 详解

    前言 在3D游戏当中,我们经常会使用到照相机这个东西,无论你使用的是哪一款引擎,都会用到,同时,照相机这个东西涉及到的东西比较多,基础知识需要扎实一些才可以. 如何使用 很久之前做项目的时候用到过一次 ...

  9. python配置文件操作——configparser模块

    # -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 Info : 配置文件ini所在 ...

  10. codevs 1191 树轴染色 线段树区间定值,求和

    codevs 1191 树轴染色 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.codevs.cn/problem/1191/ Des ...