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. JSP笔记01——尝试

    JSP ————> servlet 我的第1个Java Web应用程序——index.jsp 我的第2个Java Web应用程序——welcome-file 我的第3个Java Web应用程序— ...

  2. SQL查询语句的执行顺序

  3. 主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)

    系统角色篇 数据结构 用户管理 Controller代码 public class SystemUserController : Controller { //public void Log() // ...

  4. CentOS 6.5 下的截图方法

    1.利用命令模式 捕获整个屏幕 : $ gnome-screenshot 截完屏之后我们可以设置自定义图片存储位置,如图: 捕获当前终端Terminal : $ gnome-screenshot -w ...

  5. INSPIRED启示录 读书笔记 - 第33章 新瓶装老酒

    精明的公司至少要手握两件“法宝” 1.对目标市场了如指掌,对现有产品的缺陷洞若观火 2.跟踪最亲的技术趋势.新技术层出不穷,让之前无法实现的方案变得可能

  6. Oracle配置文件

    在oracle安装目录$HOME/network/admin下,,经常看到sqlnet.ora tnsnames.ora listener.ora这三个文件,除了tnsnames.ora,其他两个文件 ...

  7. oracle 导出

    一.导出csv文件 利用Oracle中的Spool缓冲池技术可以实现Oracle数据导出到文本文件. 1).在Oracle PL/SQL中输入缓冲开始命令,并指定输出的文件名: spool /tmp/ ...

  8. 分布式任务调度平台XXL-Job集群版搭建

    如果集群: 保存后: 启动两个 job 第二个job的配置: # web port server.port=8082 # log config logging.config=classpath:log ...

  9. delphi中httpencode使用注意事项

    delphi中httpencode使用注意事项 一.uses HTTPApp二.使用前要用UTF8Encode转换成utf-8编码HTTPEncode(UTF8Encode(Text));不然和标准的 ...

  10. Solr 报错:java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody

    HBase协处理器中使用Solr时报错,如下 2018-07-11 17:06:14,054 INFO [LruBlockCacheStatsExecutor] hfile.LruBlockCache ...