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

If it is overflow, return MAX_INT.

题解:

  思路就是被除数减去除数,减尽为止。优化的方法是尽量少的做减法。由于不能用乘法,可以利用位操作,左移一位即为该数乘上2

Solution 1

 class Solution {
public:
int divide(int dividend, int divisor) {
if (divisor == || (dividend == INT_MIN && divisor == -)) return INT_MAX;
long long m = abs((long long)dividend);
long long n = abs((long long)divisor); int sign = (dividend < ) ^ (divisor < ) ? - : ;
int res = ;
while (m >= n) {
long long tmp = n, p = ;
while (m >= (tmp << )) {
tmp <<= ;
p <<= ;
}
m -= tmp;
res += p;
}
return sign == ? res : -res;
}
};

Solution 2

 class Solution {
public:
int divide(int dividend, int divisor) {
if (!divisor || dividend == INT_MIN && divisor == -)
return INT_MAX;
int sign = ((dividend < ) ^ (divisor < )) ? - : ;
unsigned dvd = abs(dividend);
unsigned dvs = abs(divisor);
int res = ;
while (dvd >= dvs) {
unsigned temp = dvs, multiple = ;
// 不可写作 dvd >= (tmp << 1),因为有可能溢出
while (dvd - temp >= temp) {
temp <<= ;
multiple <<= ;
}
dvd -= temp;
res += multiple;
}
return sign * res;
}
};

【LeetCode】029. Divide Two Integers的更多相关文章

  1. 【LeetCode】29. Divide Two Integers

    题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...

  2. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  3. 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers

    题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  4. 【LeetCode】Sum of Two Integers

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

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

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

  6. 【leetcode】1291. Sequential Digits

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

  7. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  8. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  9. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

随机推荐

  1. 十位用户唯一ID生成策略

    新浪微博和twitter 等系统都有一窜数字ID来标示一个唯一的用户,这篇文章就是记录如何实现这种唯一数字ID 原理:使用MYSQL 自增ID 拼接任意字符..然后使用进制转换打乱规则 一般来说实现唯 ...

  2. 关于java登录界面

    import java.awt.*; 包含用于创建用户界面和绘制图形图像的所有类.   import javax.swing.*; 提供一组“轻量级”(全部是 Java 语言)组件,尽量让这些组件在所 ...

  3. PHP汉子转拼音

    <?php /** +------------------------------------------------------ * PHP 汉字转拼音 +------------------ ...

  4. [转]Navicat for oracle 提示 cannot load oci dll,193的解决方法 orcale 11g

    Navicat for oracle 提示 cannot load oci dll,193的解决方法   内网有一台windows server 2012,安装了Navicat 11.1.8 连接or ...

  5. python + Streaming框架的MR实践与优化

    Streaming是Hadoop提供的一个可以使用其他编程语言来进行MR编程的API,它使用Unix标准输入输出作为Hadoop和其他编程语言的开发接口,非常轻便.而开发者可以选择自己擅长的编程语言, ...

  6. Shell编程之Linux信号及信号跟踪

    一.Linux信号 1.什么是信号? Linux信号是由一个整数构成的异步消息,它可以由某个进程发给其他进程,也可以在用户按下特定键发生某种异常事件时,由系统发给某个进程. 2.信号列表 [root@ ...

  7. finally中的return

    周五晚6点下班去面试,出了一份笔试题,看到第一题有些蒙了,虽然以前遇到过类似的问题,但并没有留心记一下,觉得没人会这样写代码,但实际上没有面试题中是有的. 1,有在try块中执行不到finally的情 ...

  8. INSPIRED启示录 读书笔记 - 第35章 情感接纳曲线

    技术接纳曲线 涉及了技术创新者.尝鲜者.早期消费大众.后期消费大众和跟随者,很少有产品能越过鸿沟——获得尝鲜者以外消费者的青睐 不同类型的用户具有不同的情感需求,除了技术接纳曲线模型描述用户外,还应该 ...

  9. setfacl设置特定目录的权限

    现有一目录是虚拟机和linux共享的,但是每次程序调用新建的文件都发现没有权限. 于是指定特定目录及其子目录下新建的文件或目录对于用户qhfz都有读写执行的权限 -R表示递归 -m表示设置文件acl规 ...

  10. python基础语法学习常见小问题

    说明:我是最近觉得python在完成很多工作中方便使用而且功能强大,想突击学习一下.用的是廖雪峰老师的教程,学习python3.X.这里是廖雪峰老师的网站链接: http://www.liaoxuef ...