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. Django模板语言之组合搜索

    url.py from django.conf.urls import url from django.contrib import admin from app01 import views url ...

  2. SpringMVC Controller 介绍(详细深刻)

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  3. 02.socket实现远程调用

    不使用webservice使用以前的知识也可以实现远程系统之间的调用.用Socket可以.实现Socket通信. 开设一个端口.ip.

  4. hdu6188 Duizi and Shunzi (贪心或者dp)

    题意 有n张牌,第i张牌上的数字是a[i].我们定义 两张数字是一样的牌 为对子.我们定义 三张数字连续的牌 为顺子.我们想把这n张牌组成尽可能多的顺子和对子.请计算并输出能组成的最多的顺子和对子的数 ...

  5. lucene 第一天

    Lucene/Solr   第一天 1. 课程计划 Lucene介绍 全文检索流程介绍 a) 索引流程 b) 搜索流程 Lucene入门程序 a) 索引实现 b) 搜索实现 分词器 a) 分词介绍 b ...

  6. c语言实践 数字特征值

    这题的要求是这样的: 这题我没做出来,我大概思路是这样的,根据输入的数字,把这个数字的每一位都分离出来,然后判断奇数还是偶数,再判断序是奇数还是偶数,最后两个奇偶性比较,输出1还是0,这个输出的1和0 ...

  7. 基于django rest framework做认证组件

    先导入要用到的类 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions ...

  8. bootstrap实现嵌套列

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 嵌套列</title> <li ...

  9. php二维数组修改键名

    最近遇到一个问题,是关于json数据提交的时候,总是报出[object object]的错误,查了晚上需要资料,大部分的说法是json数据格式不规范导致的错误.一般建议说将dataType类型注释掉. ...

  10. css总结12:CSS Float(浮动)

    1 定义:CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. 2 解释:浮动的元素,浮在界面上,脱离了HTML文本流. 元素的水平方向浮动,意味着元素只能左右移动而不能 ...