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. 解决mybatis中转义字符的问题

    xml格式中不允许出现类似“>”这样的字符,有如下两种解决方法 方法一:使用转义字符 SELECT * FROM test WHERE 1 = 1 AND start_date <= CU ...

  2. 配置环境是程序员的第一步 -- Xshell 6 免费版下载安装

    Xshell 是一个强大的安全终端模拟软件,通常用来连接云主机,远程控制云主机. 很多人都不知道 Xshell 有专门为家庭和学校用户提供的免费版,只需要填个用户名和邮箱即可. 免费版链接:https ...

  3. resin的几个常用配置

    参考原文:http://blog.csdn.net/johnson1492/article/details/7913827 本文着重介绍resin的几个常用配置 注: 1. 本文并非resin.con ...

  4. [luogu3369]普通平衡树(treap模板)

    解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  5. linux操作小技巧

    巧妙利用别称 alias,让工作更有效率 在我的个人目录下/home/zdwu,打开.bashrc文件进行修改: 将  ll='ls -alF' 改为 ll='ls -ahlF',是的观察的结果显示更 ...

  6. 解决selenium与firefox版本不兼容问题

    Python环境下类比 个人使用 32位环境 Python 2.7.12 Selenium 2.53.6 Firefox 47.01 安装selenium可用pip选择对应版本,参考另一教程. 因为在 ...

  7. spark源码阅读之network(3)

    TransportContext用来创建TransportServer和TransportclientFactory,同时使用TransportChannelHandler用来配置channel的pi ...

  8. redis的一些简介

    Redis是Remote Dictionary Server的缩写,他本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库. 1.       redis的数据类型: st ...

  9. (转)Web API 强势入门指南

    原文地址:http://www.cnblogs.com/developersupport/p/aspnet-webapi.html Web API是一个比较宽泛的概念.这里我们提到Web API特指A ...

  10. (转)QueryBuilder : 打造优雅的Linq To SQL动态查询

    原文地址:http://www.cnblogs.com/coolcode/archive/2009/09/28/IQueryBuilder.html 首先我们来看看日常比较典型的一种查询Form 这个 ...