题意:不用乘除求余运算,计算除法,溢出返回INT_MAX。

  首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候。

  不用乘除求余怎么做?

  一.利用减法。

    耗时太长,如果被除数是INT_MIN,除数是1的时候,要循环-INT_MIN次

  二.利用位运算

  思路来自:http://blog.csdn.net/whuwangyi/article/details/40995863

  代码:

  

 int divide(int dividend, int divisor) {
int i=;
long ret=;
int flag=;
long dividend_copy=dividend;
long divisor_copy=divisor;
if((dividend<&&divisor>)||(dividend>&&divisor<))
flag=-;
dividend_copy=dividend<?((-)*dividend_copy):dividend_copy;
divisor_copy=divisor<?((-)*divisor_copy):divisor_copy;
while(dividend_copy>=divisor_copy<<){
divisor_copy<<=;
i++;
}
while(i>=){
if(dividend_copy>=divisor_copy){
dividend_copy-=divisor_copy;
ret+=<<i;
}
i--;
divisor_copy>>=;
}
ret = (flag<)?((-)*ret):ret;
if(ret==INT_MAX+)
return INT_MAX;
else
return (int)ret;
}

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

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

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

  2. 【LeetCode】029. Divide Two Integers

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

  3. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  4. 【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 ...

  5. 【LeetCode】Sum of Two Integers

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

  6. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

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

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

  9. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. Extension+NVelocity

    VS Extension+NVelocity系列(二)——让VS支持 NVelocity的智能提示(上)   一.基础概念 应该庆幸的是,VS的插件是靠着MEF实现而不是MAF,这让你所做的工作减轻了 ...

  2. 在SQL Server中添加Linked Server 图解版

    在开发中,经常需要一个SQL Server服务器去访问另一个服务器,微软提供了一种方式Linked Server 下面是配置流程: 1).打开Server Objects下 Linked Server ...

  3. [置顶] 网页提交方式post和get的区别和联系

    都知道GET一般用于获取/查询资源信息,而POST一般用于更新资源信息.但具体区别呢?可以互换吗? 主要区别有:1传输数据的方式不同,2传输的数据多少不同,3传输数据的安全性不同4,后台获取数据的方式 ...

  4. RoleManager 进行角色管理

    ASP.NET Identity 使用 RoleManager 进行角色管理 (VS2013RC) 注:本文系作者原创,但可随意转载. 最近做一个Web平台系统,系统包含3个角色,“管理员, 企业用户 ...

  5. C add

    C++学习--应用篇(Windows/Linux)(书籍推荐及分享) 我将以平台划分,分别介绍Windows和Linux下个人认为的好书(与基础篇一样,大部分网络上都有电子版): 对于C++基础类的图 ...

  6. ARC forbids explicit message send of 'autorelease'错误

    (ARC forbids explicit message send of 'autorelease'错误) 在ios中经常会遇到:ARC forbids explicit message send ...

  7. 解决GOOGLE不能用的办法

    解决GOOGLE不能用的办法 首先平时用用百度还是足够了,但是说实话,百度在进行一些尝试搜索时真的没GOOGLE好用,经常找到一大堆广告,却不是自己想要的,比如搜索里面的双引号.and.site.fi ...

  8. 依赖注入(IOC)二

    依赖注入(IOC)二 上一章我们讲了构造注入与设值注入,这一篇我们主要讲接口注入与特性注入. 接口注入 接口注入是将抽象类型的入口以方法定义在一个接口中,如果客户类型需要获得这个方法,就需要以实现这个 ...

  9. SVN 在 Xcode中的状态说明

    最近同事总是问我关于SVN状态的问题,‘C’是什么意思啦?‘A’是什么意思啦?等等一系列问题. 为了方便以后查阅,以及新同事的快速融入,特在此记录一下^_^. 当然了大家也可以google一下,一搜一 ...

  10. CLR_Via_C#事件

    CLR_Via_C#学习笔记之事件   一:首先我先引用网上别人对事件的一些说明,然后将会通过一个事例进行对事件的演示: EventArgs是包含事件数据的类的基类,用于传递事件的细节.EventHa ...