题意:不用乘除求余运算,计算除法,溢出返回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. 【转】Android实现推送方式解决方案

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  2. UVA 408 (13.07.28)

     Uniform Generator  Computer simulations often require random numbers. One way to generatepseudo-ran ...

  3. 适合编写代码的字体 Source Code Pro

    今天看到博客园一篇文章,介绍了一种出身自 Adobe 的适合编码的字体,等宽,支持ClearType等,试用一下吧 项目地址:https://github.com/adobe/source-code- ...

  4. java实现验证码

    第一步:在web.xml中配置servlet <servlet> <servlet-name>ImageServlet</servlet-name> <ser ...

  5. Introduction to SharePoint hierarchy

    /* Author: Jiangong SUN */ I've participated in a SharePoint 2010 project to build an intranet. Base ...

  6. txt文件保存问题

    今天终于解决了文件的保存问题,之前删除文件,再保存,发现删除的内容还是在文件中. 这是因为保存内容时,是将数组内容一行一行覆盖原来的内容,没有覆盖到的还是保存在文件内. 我用的是CFile类 所以直接 ...

  7. 纯Python综合图像处理小工具(3)10种滤镜算法

    <背景>  滤镜处理是图像处理中一种非常常见的方法.比如photoshop中的滤镜效果,除了自带的滤镜,还扩展了很多第三方的滤镜效果插件,可以对图像做丰富多样的变换:很多手机app实现了实 ...

  8. 11 款最好 CSS 框架

    11 款最好 CSS 框架 让你的网站独领风骚 网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒 ...

  9. HTML如何转XTML

    ob_start(); $html = curl_init('http://www.beijing.gov.cn/'); curl_exec($html); $html = iconv('GBK',' ...

  10. [置顶] github 出现 Permission denied (publickey)的解决

    今天写了一篇博客,想push到github上的时候出现了以下错误 Permission denied (publickey). fatal: The remote end hung up unexpe ...