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. 【BZOJ】3319: 黑白树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种: ...

  2. OI分类

    黑字:认识 红字:要学 未添加:要学 ├─模拟├─字符串│    ├─字符串基础│    ├─manacher│    ├─kmp│    ├─trie│    ├─ac自动机│    ├─后缀数组( ...

  3. Mysql_mysql force Index 强制索引

    其他强制操作,优先操作如下: mysql常用的hint 对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的h ...

  4. 1.Java为什么能跨平台运行?请简述原理。

    使用不同操作系统的JVM(JAVA虚拟机)解释运行编译好的字节码文件(.class)

  5. Oracle学习整理

    用 startup 命令启动数据库,用 shutdown 命令关闭数据库 Oracle SQL 语句由如下命令组成:  数据定义语言(DDL),包括 CREATE (创建)命令.ALTER (修改) ...

  6. php5全版本绕过open_basedir读文件脚本

    这是前段时间写的代码了(http://www.weibo.com/1074745063/ByAPqj7s0),最近一直忙着和几个同学一起做非安全类的创业项目.所以也没拿到JAE.SAE测试一下. 不说 ...

  7. PHP统计字符串里单词查询关键字

    <?function full_count_words($str) {     //返回完整数组,包含字符串里每个单词 $words = str_word_count($str,1);     ...

  8. [GE]手动截取当前活动窗口,并且按规则命名(1/2)

    Function Take-ScreenShot { <# .SYNOPSIS Used to take a screenshot of the desktop or the active wi ...

  9. 动态input file多文件上传到后台没反应的解决方法!!!

    其实我也不太清除具体是什么原因,但是后面就可以了!!! 我用的是springMVC 自带的文件上传 1.首先肯定是要有springMVC上传文件的相关配置! 2.前端 这是动态input file上传 ...

  10. ps命令交叉编译

    busybox中的ps命令是针对于嵌入式的,其中一些选项并不完整.因此需要将源码下载下来,进行交叉编译 官方下载地址 github下载地址 含有configure,我在此使用的是这个源码包,官方的包在 ...