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

常常出现大的负数,无法用abs()转换成正数的情况

class Solution{
private:
vector<long long> f;
public:
int bsearch(vector<long long> &a,int left,int right,long long key){
if(left > right)
return -; int mid = left + (right-left)/;
if(a[mid]==key)
return mid;
else if(a[mid]<key){
int pos = bsearch(a,mid+,right,key);
return pos == - ?mid :pos;
}else
return bsearch(a,left,mid-,key);
} int divide(int devidend,int divisor){
int sign = devidend < ?-:;
if(divisor<)
sign *= -; long long div = devidend;
div = abs(div);
long long divisorL = divisor;
divisorL = abs(divisorL);
f.push_back(divisorL);
int size = ; while(true){
if(f[size-] >= div)
break;
f.push_back(f[size-]+f[size-]);
size++;
}//end while int num = ;
long long sum = ;
while(div>){
int pos = bsearch(f,,size-,div);
if(pos == -)
break;
div -= f[pos];
num += (<< pos);
}//end while
return num*sign;
}
};

[LeetCode] Divide Two Integers( bit + 二分法 )的更多相关文章

  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 Divide Two Integers

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

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

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

  5. leetcode Divide Two Integers python

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

  6. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  7. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  8. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  9. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

随机推荐

  1. ural 1306. Sequence Median

    1306. Sequence Median Time limit: 1.0 secondMemory limit: 1 MBLanguage limit: C, C++, Pascal Given a ...

  2. 操作properties文件,注意抹掉最前面的"file:"

    package com.xiewanzhi.property; import java.io.BufferedInputStream; import java.io.File; import java ...

  3. Codeforces Round# 305 (Div 1)

    [Codeforces 547A] #include <bits/stdc++.h> #define maxn 1000010 using namespace std; typedef l ...

  4. Spark RDD Operations(1)

    以上是对应的RDD的各中操作,相对于MaoReduce只有map.reduce两种操作,Spark针对RDD的操作则比较多 ************************************** ...

  5. opengl中拾取操作的实现

    opengl采用一种比较复杂的方式来实现拾取操作,即选择模式.选择模式是一种绘制模式,它的基本思想是在一次拾取操作时,系统会根据拾取操作的参数(如鼠标位置)生成一个特定视景体,然后又系统重新绘制场景中 ...

  6. 2015寒假ACM训练计划

    1月26号至3月4号 每天给自己一个计划.做有意义的事情,不要浪费时间. 8:00——11:30 acm训练 11:30——13:00 午休 13:00——17:30  acm训练 17:30——18 ...

  7. 30个非常流行的提示信息插件(jQuery Tooltip Plugin)

    在网站的设计中,提示信息是非常细微的功能,但是起着非常重要的作用.如果你的网站中提示信息做的比较好,会给浏览者留下非常深刻的印象,同时也会起到非常好的网站宣传效果,下面介绍了30个比较流行提示信息插件 ...

  8. PHP Execute Command Bypass Disable_functions

    先简单说一下php调用mail()函数的过程. 看到源码ext/mail.c 236行: char *sendmail_path = INI_STR("sendmail_path" ...

  9. Coremail邮件系统存储型XSS两个

    (1):Coremail邮件系统存储型XSS之一 给受害者发送主题如下的邮件: <svg onload='img=new Image();img.src="//x55.me/geo.p ...

  10. $_ 与 $PSItem

    PowerShell 3.0 中的$PSItem 此文章于2012年11月4日发表在PowershellPowershell小技巧并加以Powershell 3.0管道的标签 by Mooser Le ...