Divide Two Integers-不用'/' '*' '%'操作实现整数的除法
题目描述:
不用 '*' '/' 和 '%' 运算实现两个整数的除法
题目来源:http://oj.leetcode.com/problems/divide-two-integers/
题目分析:
例如 16 / 3,可以按照3的倍数来操作。 3 * 1 = 3, 3 * 2 = 6, 3 * 2 * 2 = 12 ...,而2 * 2等可以用位运算方式表示。
那么有16 - 3 = 13, 13 - 3 * 2 = 7, 而7 < 3 * 2 *2。可以迭代处理,再计算剩下的 7 / 3,依次进行。
位运算的典型应用
示例代码:
int divide(int dividend, int divisor) {
long long a = abs((double)dividend), b = abs((double)divisor), ret = ;
while(a >= b) {
for(long long c = b, i = ; a >= c; ++i, c <<= ) {
a -= c;
ret += << i;
}
}
return ((dividend ^ divisor) >> ) ? -ret : ret;
}
Divide Two Integers-不用'/' '*' '%'操作实现整数的除法的更多相关文章
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...
- [leetcode]29. Divide Two Integers不用除法实现除法
思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大. 递归实现 剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将 ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode(29)Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
随机推荐
- 【转】php和java之间rsa加密互通
以下是php封装好的类,引入即可使用 <?php /** * 作者:pjp * 邮箱:vippjp@163.com */ class RSA{ private $privateKey='';// ...
- struts2的 defalut-action-ref 的使用
这个配置的用法有值得注意的地方,所以才记录下来: 一般default-action-refer配置的action是在浏览器中输入的网址只输入到项目时或输入错误的action时 所进入的action,一 ...
- 九度OJ 1053:互换最大最小数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6613 解决:2676 题目描述: 输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后输出. 输入: 测试数据有多组 ...
- 题解 CF97C 【Winning Strategy】
题解 CF97C [Winning Strategy] 此题是某平台%你赛原题,跟大家分享一下某校zsy和sxr等同学的神仙做法. 我解释一下题意,大是说,我有[无限]个人,每个人可以对他" ...
- 我的Android进阶之旅------>使用ThumbnailUtils类获取视频的缩略图
今天看了一段代码,是关于获取视频的缩略图的,让我认识了一个ThumbnailUtils类,代码如下. Bitmap bitmap = ThumbnailUtils.createVideoThumbna ...
- 深入了解Cookie(1)------selenium2进行Cookie操作的前奏
世界上最宽阔的是海洋,比海洋还宽阔的是天空,比天空还宽阔的是人的心量.做人的心量有多大.人生的成就就有多大. 不为一己之利去争.去斗.去夺,扫除报复之心和妒忌之念.自然"心底无私天地宽&qu ...
- ABAP excel操作 OLE 常用方法和属性
转自 http://bstone.blog.163.com/blog/static/176820446201172834149199/#userconsent# OLE 常用方法和属性 1.ole中如 ...
- API的理解和使用——全局命令
全局命令 命令 功能 set 创建键值对 keys 遍历查看所有键 exists 判断一个键是否存在,1存在,0不存在 dbsize 当前数据库中有多少个键 del 删除一个或多个键 expire 设 ...
- SQL Server分区表,能否按照多个列作为分区函数的分区依据(转载)
问: Hi, I have a table workcachedetail with 40 million rows which has 8 columns.We decided to partiti ...
- drawable animation基础
动画配置<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android: ...