题意:不用乘除求余运算,计算除法,溢出返回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. PHP中的表单提交和获取

    在php中表单提交的方式有两种: 1.post提交,这种安全性较高. 2.get提交,他提交的是一个url地址,因此在从地址上面就可以看到许多信息,因此不安全. 每个表单<form>后面都 ...

  2. NFC高级

    高级 NFC 本文档介绍了高级的NFC主题,如各种标签技术,NFC标签写入和前台发布,它允许即使当其他应用程序过滤器相同的时候,应用程序在前台处理Intent. Tag技术支持工作 当使NFC Tag ...

  3. MINIGUI 编译 helloworld

    MiniGui 编译hello.c 文件成功!记载一下! MiniGui 版本v3.0 和 2 编译 差异 是极其的大!   源文件代码 :   #include <stdio.h>#in ...

  4. ASP.NET Web API的HttpController是如何被激活的?

    ASP.NET Web API的HttpController是如何被激活的? HttpController与HttpControllerDescriptor 程序集的解析 HttpController ...

  5. 关于Dictionary字典和List列表

    命名空间System.Collections.Generic中有两个非常重要,而且常用的泛型集合类,它们分别是Dictionary<TKey,TValue>字典和List<T> ...

  6. [转]Blocking Code Injection on iOS and OS X

    Source:http://www.samdmarshall.com/blog/blocking_code_injection_on_ios_and_os_x.html Yesterday I pos ...

  7. HTML5 前端框架和开发工具【下篇】

    HTML5 前端框架和开发工具[下篇] 快速,安全,响应式,互动和美丽,这些优点吸引更多的 Web 开发人员使用 HTML5.HTML5 有许多新的特性功能,允许开发人员和设计师创建应用程序和网站,带 ...

  8. kindeditor图片上传 struts2实现

    一.kindeditor以及struts2部署搭建不再赘述,如需要请参考kindeditor使用方法 Struts2框架搭建 二.kindeditor图片上传所依赖jar包在kindeditor\js ...

  9. Android: Type Method 'NewStringUTF' could not be resolved

      编译一个最简单的Android NDK 例子(android-ndk-r8/samples/hello-jni)出现了错误信息:Type Method 'NewStringUTF' could n ...

  10. ThinkPHP中连接mysql数据库的四种实用和通用的连接方法

    ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库适配器来处理.目前的数 ...