[LeetCode] Divide Two Integers( bit + 二分法 )
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 + 二分法 )的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
随机推荐
- Android 蓝牙 BLE 开发笔记
最近公司头戴换了一块蓝牙4.0 BLE模块,所以我们Android组要适配 BLE.Android BLE 需要 4.3 以上系统,api 还是非常简单的, 第一步就是扫描, 扫描到设备后就可以连接了 ...
- TYVJ P1073 加分二叉树 Label:区间dp
背景 NOIP2003 提高组 第三道 描述 设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第j个节点的 ...
- POJ 3071 Football(概率DP)
题目链接 不1Y都对不住看过那么多年的球.dp[i][j]表示i队进入第j轮的概率,此题用0-1<<n表示非常方便. #include <cstdio> #include &l ...
- JQuery图形插件,Highcharts平滑线条处理方法
第一种:静态数据 $('#THChartDiv').highcharts({ chart: { type: 'spline' }, title: { text:过程线' }, xAxis: { tit ...
- Java递归搜索指定文件夹下的匹配文件
import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Queue; /** ...
- Configuration of OpenCV2.1.0 with VS2010
Add in the system Path: C:\Program Files (x86)\OpenCV-2.1.0\build\bin\Debug Project->Project Prop ...
- C++盲点
const const指针 char greeting[] ="hello"; char* p = greeting; // non-const pointer, non-cons ...
- 如何处理NBU Frozen Media
https://support.symantec.com/en_US/article.TECH21473.html
- Win2003+Powershell2.0下无权限解锁账号
在GE环境下,我通过图形界面ADUC可以解锁一个员工账号,但是通过powershell命令却提示权限不足,咋回事? PS C:\Users\814072> Unlock-ADAccount ...
- ci文件缓存使用,可以用来实现多模板切换 改写URL辅助函数
//2015年2月28日15:13:41 $this->load->driver('cache', array('adapter' => 'file'));//加载适配器 //请注意 ...