题目:

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. 使用PHP解压文件Unzip

    这是一个非常方便的PHP函数从.zip文件解压缩文件.它有两个参数:第一个是压缩文件的路径和第二 function unzip_file($file, $destination) { // creat ...

  2. PHP正则表达式屏蔽电话号码中间段

    要屏蔽电话号码中间段,首先要知道电话号码的正则表达式. 先来看看PHP匹配电话号码的正则表达式. 匹配固定电话的正则表达式为: /(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\- ...

  3. PHP 中const 与define 区别

    1.const用于类成员变量定义,一旦定义且不能改变其值.define定义全局常量,在任何地方都可以访问. 2.define不能在类中定义而const可以. 3.const不能在条件语句中定义常量 i ...

  4. Hadoop学习历程(四、运行一个真正的MapReduce程序)

    上次的程序只是操作文件系统,本次运行一个真正的MapReduce程序. 运行的是官方提供的例子程序wordcount,这个例子类似其他程序的hello world. 1. 首先确认启动的正常:运行 s ...

  5. Linux中的段管理,bss段,data段,

    Linux 的段管理, BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存 ...

  6. mark 的总结开发笔记-备

    2.播放音乐:-(void) playMusic{@try{//取文件路径NSString *musicFilePath = [[NSBundle mainBundle] pathForResourc ...

  7. GO的数组及切片

    感觉在向PYTHON学一些数组方面的功能. package main import "fmt" func main() { ]], , , , , , , , , } ] fmt. ...

  8. Manage Spring Boot Logs with Elasticsearch, Logstash and Kibana

    下载地址:https://www.elastic.co/downloads When time comes to deploy a new project, one often overlooked ...

  9. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

    1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved:  ...

  10. hdu3849-By Recognizing These Guys, We Find Social Networks Useful:双连通分量

    By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/Others)     ...