【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 ...
随机推荐
- mysql查询字段值为数字
原文:mysql查询字段值为数字 我想查询字段值为数字的sql如下:select * from tj_item_result where tj_value REGEXP '^[0-9]'
- sql like 时间需要做转换
EG: where Convert(varchar(100),[字段名],120) like '2010-10-10%'
- 应用CSS的page-break-after属性 实现WEB页面强制分页打印
虽然dedecms.com向大家介绍了很多CSS属性的相关知识,但有些非常冷门的属性还是有所欠缺.在B/S程序中,对打印页面的控制,CSS相对比较弱,例如: 自动分页, 就基本没啥实际用途.我们通常需 ...
- 使用POI导出excel
引言:对于excel的导出,首先是将数据写到WorkBook中,然后将book以流的形式写出即可,看代码: public void exportResultInfo(String fileName,S ...
- UIWebView的探索
UIWebView 说到iOS的UIWebView,应该会很快回忆起常用委托方法,异步loadRequest.stopLoading.reload方法等. 在此我总结一些容易忽略的属性和方法: 1. ...
- java判断字符串是否为乱码
项目中有一个功能 在IE中GET方式提交会产生乱码 但有两个入口都会走这同一段代码 固不能直接转码,所以要进行判断传过来的该值是不是乱码 可用以下方式验证: java.nio.charset.Char ...
- 设计师和开发人员更快完成工作需求的20个惊人的jqury插件教程(上)
[转] 设计师和开发人员更快完成工作需求的20个惊人的jqury插件教程(上) jquery的功能总是那么的强大,用他可以开发任何web和移动框架,在浏览器市场,他一直是占有重要的份额,今天,就给大家 ...
- .net postsharp编译时生成的代码?
使用PostSharp进行AOP框架设计:一个简单的原型 AOP已经不是一个什么新名词了,在博客园使用关键字搜索可以查出n多条关于AOP的介绍,这里就不再赘述了. 在Bruce Zhang's B ...
- CSS学习小记
搜狗主页页面CSS学习小记 1.边框的处理 要形成上图所示的布局效果,即,点选后,导航下面的边框不显示而其他的边框形成平滑的形状.相对于把导航的下面边框取消然后用空白覆盖掉下面搜索栏的边框比较而言 ...
- .NET中 类型,对象,线程栈,托管堆在运行时的关系
.NET中 类型,对象,线程栈,托管堆在运行时的关系 The Relationship at Run Time between Types,Objects,A Thread's Stack,and T ...