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

If it is overflow, return MAX_INT.

两数相除,不能用*,/,%  那么只能用位移运算了,注意边界条件防止溢出,代码如下:

 class Solution {
public:
int divide(int dividend, int divisor) {
int sign1 = dividend > ? : -;
int sign2 = divisor > ? : -;
long long d1 = abs(static_cast<long long>(dividend));
long long d2 = abs(static_cast<long long>(divisor));
int res = ;
while(d1 >= d2){
long long tmp = d2;
for(int i = ; d1 >= tmp; ++i){
d1 -= tmp;
tmp <<= ;
res += 1LL << i;
}
}
if(sign1 == sign2){
if(res == INT_MIN)
return INT_MAX;
return res;
}else
return res * -;
}
};

PS:这题溢出情况相当恶心。

LeetCode OJ:Divide Two Integers(两数相除)的更多相关文章

  1. [LeetCode] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  2. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  3. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  4. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  5. [LintCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  6. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  7. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. LeetCode(29): 两数相除

    Medium! 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor ...

  9. 【LeetCode每天一题】Divide Two Integers(两整数相除)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  10. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

随机推荐

  1. mongo distinct 指定条件

    db.Article.distinct("字段名称",{"Comment.Reply.email" : "xxx"})

  2. R中的data.table 快速上手入门

    data.table包提供了一个非常简洁的通用格式:DT[i,j,by]. 可以理解为:对于数据集DT,选取子集行i,通过by分组计算j. 对比与dplyr等包,data.table的运行速度更快. ...

  3. sublime txet 3 python 开发环境安装配置

    下载python 下载地址:https://www.python.org/downloads/windows/ 下载sublime text 3 下载地址:https://www.sublimetex ...

  4. matlab循环保存dat文件

    将数据保存为dat文件 这里有两种方法,第一种是: save filename dataname; 这种方法书写简单,但是功能也很简单.这里的filename就是死的filenam,即filename ...

  5. begoo——对象的CRUD操作

    如果已知主键的值,那么可以使用这些方法进行CRUD操作 对object操作的四个方法Read/Insert/Update/Delete o := orm.NewOrm() user := new(Us ...

  6. SHELL —— grep命令+正则表达式

    一 什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 生活中处处都是正则: 比如我们描述:4条腿 你可能会想 ...

  7. CodeForces - 451E Devu and Flowers (容斥+卢卡斯)

    题意:有N个盒子,每个盒子里有fi 朵花,求从这N个盒子中取s朵花的方案数.两种方法不同当且仅当两种方案里至少有一个盒子取出的花的数目不同. 分析:对 有k个盒子取出的数目超过了其中的花朵数,那么此时 ...

  8. Linux网络相关命令firewalld和netfilter、iptables 使用(6/22)

    iptables和netfilter的关系: netfilter在内核空间的代码根据table中的rules,完成对packet的分析和处置.但是这些table中的具体的防火墙rules,还是必须由系 ...

  9. javascript-实现小抽奖程序

    直接上代码 <style> *{ margin: 0; padding:0;} .prize_wrap{ width: 300px; height: 150px; } .prize_wra ...

  10. 解决Vim插入模式下backspace按键无法删除字符的问题【转】

    本文转载自:https://blog.csdn.net/zxy987872674/article/details/64124959 最近使用某个服务器编辑文件时,快捷键i进入插入模式后,下方不出现in ...