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

 class Solution {
public:
int divide(int dividend, int divisor) {
long long a = dividend < ? -(long long)dividend : dividend;
long long b = divisor < ? -(long long)divisor : divisor;
bool sign = (dividend >= && divisor >= ) ||
(dividend <= && divisor <= );
long long ans = ;
while (a >= b) {
long long c = b;
for (int i = ; c <= a; c = c << , ++i) {
a -= c;
ans += ( << i);
}
}
return sign ? ans : -ans;
}
};

不能用乘、除和取模,那么还可以用加、减和移位。

符号单独考虑。主要从减法出发,可以通过移位加倍被除数从而实现加速。有一点要特别注意的是可能发生溢出,需要用long long类型来进行中间的运算。

【Leetcode】Divide Two Integers的更多相关文章

  1. 【leetcode】Divide Two Integers (middle)☆

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

  2. 【Leetcode】 - Divide Two Integers 位运算实现整数除法

    实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...

  3. 【Leetcode】【Medium】Divide Two Integers

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

  4. 【LeetCode】970. Powerful Integers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...

  5. 【leetcode】970. Powerful Integers

    题目如下: Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j fo ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. 【LeetCode】二分 binary_search(共58题)

    [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...

  8. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  9. 【leetcode】1291. Sequential Digits

    题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...

随机推荐

  1. Pacemaker实现双机热备

    在互联网高速发展的今天,尤其在电子商务的发展,要求服务器能够提供不间断服务.在电子商务中,如果服务器宕机,造成的损失是不可估量的.要保证服务器不间断服务,就需要对服务器实现冗余.在众多的实现服务器冗余 ...

  2. Oracle 环境下 GoldenGate 集成抽取(Integrated Capture)模式与传统抽取模式(Classic Capture)间的切换

    检查抽取进程模式 在 GGSCI 环境下,执行类似如下语句查看特定进程的状态. GGSCI> info <Group_Name> 其中,<Group_Name> 为进程名 ...

  3. 在TFS中获取项目,获取最新和上传(迁入)

    一.在TFS中获取项目: (1)team (2)connect to team.... (3)选择服务地址,然后close (4)然后映射本地文件夹下载项目 (5)mapped添加映射,在map下就可 ...

  4. c# 判断网络地址是否存在

    方法一:网络地址存在,有可能可以访问,也有可能不能访问.此方法用来判断地址存在. static bool UrlIsExist(String url) { System.Uri u = null; t ...

  5. c语言实践 打印字母三角形

    效果如下: 我是怎么想的: 总共需要打印6行字母,那么就需要一个循环来控制打印第几行,大概代码如下: for(int i=0;i<6;i++) { } 每行都会打印字母,而且循环越往后,需要打印 ...

  6. 19. AUTO INCREMENT 字段

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  7. 复习扩展方法 涉及委托,这里我使用自定义委托类型 public delegate bb MyFunc<in T,out bb> (T arg)

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...

  8. sublime text3安装后html:5+Tab不能快速生成html头部信息的解决办法

    sublime text3安装后html:5+Tab不能快速生成html头部信息的解决办法: 需要下载Emmet插件,按网上写的步骤按ctrl+shift+P打开命令面板,输入install,鼠标点击 ...

  9. C++的惨痛教训(未完待续)

    题记:只有痛才能让人铭记!痛促进进步~ 1. strncpy,大家都知道要做安全检查,可是谁都有嫌麻烦的时候,尤其是自己很自信不会产生溢出的时候,可能不会坑了自己,却会坑了使用这段代码的人.所以,1. ...

  10. 关于 href="\\#" 和 return false

    href="\\#"  跳转到本页 return false 相当于不刷新 href="javascript:void(0)"   或者 href=" ...