【LeetCode】29. Divide Two Integers
题意:不用乘除求余运算,计算除法,溢出返回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的更多相关文章
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【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 ...
- 【LeetCode】Sum of Two Integers
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- CLR Profiler 性能分析工具
CLR Profiler 性能分析工具 CLR Profiler 性能分析工具 CLR Profiler 有两个版本,分别用于CLR1.1 和 CLR2.0,至于CLR4试了一些也可以,但不知道是否完 ...
- Asp.Net MVC5入门学习
添加一个Controller(控制器) 因为我们用的是Asp.Net MVC,MVC最终还是一套框架,所以我们还是需要遵循它才能玩下去,或者说是更好的利用来便于我们的开发,要是对MVC概念还有点模糊的 ...
- openGL剪裁区
/** * 缓冲区工具类 */public class BufferUtil { /** * 将浮点数组转换成字节缓冲区 */ public static ByteBuffer arr2ByteB ...
- Blend4开发:会飞的小鸟
1,绘图,动画制作: 2,运行截图: 源码:http://files.cnblogs.com/yuanli/Animals.zip 动画效果参见源码.
- .net开发框架设计
转WisDom .net开发框架设计 WisDom .net 框架设计 1. 为啥要弄 2014 年我已经是我们参加工作的第六年,也做过不少项目,但是发现自己没有代码积累.这里利用业余时间梳理一下 ...
- iOS基础 - UITableView的数据源和代理
一.UITableView的代理方法 #pragma mark 每一行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtI ...
- asp.net mvc4 运用 paypal sdk实现支付
1.https://developer.paypal.com/ 注册账号,并且申请一个app,获得 client id,secret等数据 2.点击页面中"Sandbox Account ...
- [转]编译 JavaScriptCore For iOS
from: http://phoboslab.org/log/2011/06/javascriptcore-project-files-for-ios github: https://github.c ...
- [RM 状态机详解1] RMApp状态机详解
概述 Apache Hadoop 2.0在Hadoop 1.0基础上做了许多的重构工作,代码上的重构最大的变化在于引入状态机处理各个角色的状态与变迁,使用状态机是得代码结构更加清晰,方便异步处理各种操 ...
- LatestResultsProvider
LatestResultsProvider 前言 阅读本文前,需要读者对happens-before比较熟悉,了解非阻塞同步的一些基本概念.本文主要为happens-before法则的灵活运用,和一些 ...