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 ...
随机推荐
- Java for LeetCode 101 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- linux下更改文件夹名
mv 旧文件夹名 新文件夹名 mv /usr/bin/python_old /usr/bin/python_new
- Java多线程系列 基础篇04 线程中断
1. 中断线程 中断可以理解为线程的一个标志位属性,它表示一个运行中的线程是否被其他线程进行了中断操作,其他线程通过调用该线程的interrupt()方法对其进行中断操作,线程通过检查自身是否被中断来 ...
- [BZOJ 1095] [ZJOI 2007]Hide 捉迷藏
在BZ上连续MLE n次后,终于A了. 自己YY的动态点分写法,思路还是很清楚的,但是比较卡内存. 用到了MAP导致复杂度比其他的代码多了一个log,看来需要去借鉴一下别人怎么写的. updata i ...
- 基于android的GPS移植调用关系【转】
本文转载自:http://blog.csdn.net/jshazk1989/article/details/6877823 版权声明:本文为博主原创文章,未经博主允许不得转载. http://down ...
- c# CODE REVIEW (13-11 TO 14-01)
一. 松耦合
- c语言学习的第12天
#include <stdio.h> int main(void) { int *p; int i=5; char ch='A'; p=&i; *p=99; printf(&quo ...
- smokeping 出现的问题
Global symbol "%Config" requires explicit package name at /usr/lib64/perl5/lib.pm line 10. ...
- <tx:advice/> 有关的设置
将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传播设置是 REQUIRED 隔离级别是 DEFAULT 事 ...
- Android SDK Manager更新问题
1.如果是windows7,那么"开始-->所有程序-->Android SDK Tools-->右键SDK Manager-->以管理员身份运行(A)"2 ...