题目:

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

思路分析

二分法.将除数不断增倍,而结果同样扩大两倍,直到除数的值大于被除数.然后再利用被除数减去除数最后增长到小于被除数的值,递归求出结果.

例如:123/4

4<123   4*2=8<123  8*2=16>123    16*2=32<123

32*2=64<123   64*2=128>123  故结果增长的值为64/4=16.

再利用123-64=59,再次递归求出结果,最后肯定能得出59/4=14,余数为3,3<4抛弃.

故最终值为16+14=30

代码:

 public Solution{
public:
long long interDivide(unsigned long long dividend,
unsigned long long divisor){
if(dividend<divisor) return ; long long result=;
unsigned long long tmp=divisor,left; while(tmp<=dividend){
left=dividend-tmp;
tmp<<=; if(tmp>dividend){
break;
} else{
result<<=;
}
} return result+interDivide(left,divisor);
} int Divide(int dividend,int divisor){
unsigned long long _dividend=abs((long long)dividend);
unsigned long long _divisor=abs((long long)divisor); bool positive=((dividend>=) && (divisor>)) || ((dividend<=) && (divisor<)); return positive?interDivide(_dividend,_divisor):(-)*interDivide(_dividend,_divisor); }
};

之所以用unsigned long long是为了防止溢出.

LeetCode-Divdend two Integers的更多相关文章

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

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

  2. LeetCode: Divide Two Integers 解题报告

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

  3. LeetCode 970. Powerful Integers (强整数)

    题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...

  4. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  5. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  6. Leetcode:Divide Two Integers分析和实现

    题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...

  7. LeetCode OJ-- Divide Two Integers *

    https://oj.leetcode.com/problems/divide-two-integers/ 在不使用乘法.除法.求余的情况下计算除法. 使用减法计算,看看减几次. 刚开始寻思朴素的暴力 ...

  8. 【LeetCode】Powerful Integers(强整数)

    这道题是LeetCode里的第970道题. 题目描述: 给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个 ...

  9. leetcode Divide Two Integers python

    class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...

  10. leetcode 29-> Divide Two Integers without using multiplication, division and mod operator

    class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...

随机推荐

  1. 关于升级到win10后的网络问题

    最近我的alienware电脑从win7升级到win10,看到很多网友都有一个问题,那就是网络受限了, 基本看了很多百度到的,方法基本都不是很有用,我看到了一个网友的办法完美解决了我机器上的网络问题, ...

  2. PHP程序员衰老后的下场

    长期从事编程活动的程序员都期望在50多岁时能爬到一个足够高的职位,或者能顺利的退休. 但我在这里讨论的可能是一个你还没有想过的问题:如果到那时你失业了呢? 50多岁时你的职业仕途会成为一个问题.如果你 ...

  3. A Simple Math Problem(HDU 1757 构造矩阵)

    If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-1 ...

  4. C 语言简历一个文件夹 并自己输入字符 来取文件夹名字

    int main(void) { FILE *fp; char ch,filename[10]; scanf("%s",filename); if((fp=fopen(filena ...

  5. SolrCloud初识

    文章摘自:http://www.bubuko.com/infodetail-923588.html 一.概述 Lucene是一个Java语言编写的利用倒排原理实现的文本检索类库: Solr是以Luce ...

  6. iOS 两种方法实现左右滑动出现侧边菜单栏 slide view

      现在很多的APP中都有slide view,左右滑动出现侧边菜单栏的功能,Weico这个应用就有. 网上有很多第三方的类库实现了这种效果,其实自己代码写的话也是很简单的,下面我将介绍两种方法实现s ...

  7. 使用typedef语句定义数组类型

    使用typedef语句定义数组类型     1. 一维数组类型的定义格式 typedef <元素类型关键字><数组类型名>[<常量表达式>]; 例如: (1) ty ...

  8. codeforces Ilya and Matrix

    http://codeforces.com/contest/313/problem/C #include <cstdio> #include <cstring> #includ ...

  9. Altium designer快捷键

    1. 先设置参数,开启高亮显示,见下图红圈处: (1)选择使能可以高亮:CTRL+鼠标左键点击相应PCB网络即可高亮 (2)选择仅切换键时高亮显示,可以在CTRL+鼠标左键点击相应PCB网络高亮后,移 ...

  10. 解决MVC项目中,静态html 未找到时候,404的跳转

    using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using ...