[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 ...
随机推荐
- 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...
- 【BZOJ】1507: [NOI2003]Editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1507 当练splay模板了,发现wjmzbmr的splay写得异常简介,学习了.orzzzzzzzz ...
- MatLab 组件大全
MATLAB 矩阵实验室 7.0.1 Simulink ...
- for循环下九九乘法表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- linux 2.6.37-3.x.x x86_64
/* * linux 2.6.37-3.x.x x86_64, ~100 LOC * gcc-4.6 -O2 semtex.c && ./a.out * 2010 sd@fuckshe ...
- IOS第四天(5:创建备份区按钮和判断结果)
创建备份区按钮和判断结果 /** 创建备选区按钮 */ - (void)createOptionButtons:(HMQuestion *)question { // 问题:每次调用下一题方法时,都会 ...
- 递归,回溯,DFS,BFS的理解和模板【摘】
递归:就是出现这种情况的代码: (或者说是用到了栈) 解答树角度:在dfs遍历一棵解答树 优点:结构简洁缺点:效率低,可能栈溢出 递归的一般结构: void f() { if(符合边界条件) { // ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2build627)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build672 编译日期:2014-12-29 ====================================== 二. ...
- MSF命令 收集
一.msfconsole ? 帮助菜单 back 从当前环境返回 banner 显示一个MSF banner cd 切换目录 color 颜色转换 connect 连接一个主机 e ...
- Windows编译安装OpenSSL
windows下使用vs2008中的nmake编译安装openssl的脚本build.bat: echo off & color 0A :: 项目名称 set PROJECT=openssl ...