[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 ...
随机推荐
- extjs 2.0获取选中的radio的值
var temp=winFormPanel.getForm().findField('selectedType').getGroupValue();
- BZOJ4155 : [Ipsc2015]Humble Captains
第一问最小割,第二问: 设du[i]表示i点的度数,则要最小化$\frac{|1集合的du[i]之和-2集合的du[i]之和|}{2}$, 压位01背包即可. #include<cstdio&g ...
- c# 纯代码方式创建快捷方式
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...
- [Unity2D]精灵
精灵是Unity2D里面对通过图片纹理实现的游戏对象,通常会是游戏里面的玩家,敌人之类的,在Unity里面创建一个精灵的操作非常简单,直接把图片资源拖放到Hierarachy视图就可以完成了精灵的创建 ...
- tyvj100题留念
全是水题萌萌哒~0~... 留个纪念
- shell运算
- iOS5中UIViewController的新方法
iOS5中UIViewController的新方法 前言 在苹果的 WWDC2011 大会视频的<Session 101 - What's New in Cocoa> 和<Sessi ...
- NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...
- [LintCode] Cosine Similarity 余弦公式
Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...
- JSValidation客户端验证框架
下载 引入文件 <script src="/Example4/js/validation-framework.js"></script> <!DOCT ...