leetcode: Divide Two Integers
来源:互联网 发布:阿里推荐算法负责人 编辑:程序博客网 时间:2024/04/30 14:24
将因子不断乘以2(可以通过移位实现,同时结果也从1开始不断移位加倍),然后和被除数比较,等到大于被除数一半了,就从被除数中减去,将因子个数叠加入结果中。然后在剩下的被除数中采用同样的方法减去小于其一半的因子和,循环往复
必须要unsigned int
class Solution {public: int divide(int dividend, int divisor) { assert( divisor != 0); int res = 0; bool neg = (( dividend ^ divisor) >> ( sizeof(int) * 8 - 1)) ? false : true; unsigned tdividend = abs(dividend); unsigned tdivisor = abs(divisor); while( tdividend > tdivisor){ int factor = 1; unsigned tmp = tdivisor; while( (tmp<<= 1) < tdividend){ factor <<= 1; } tdividend -= ( tmp >>= 1); res += factor; } if( tdividend == tdivisor) ++res; return neg ? res : -res; }};
0 0
- LeetCode: Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode] Divide Two Integers
- [Leetcode] Divide Two Integers
- LeetCode Divide Two Integers
- 【leetcode】Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode-Divide Two Integers
- LeetCode: Divide Two Integers
- Divide Two Integers - leetcode
- Leetcode: Divide Two Integers
- LeetCode:Divide Two Integers
- leetcode Divide Two Integers
- LeetCode Divide Two Integers
- LeetCode | Divide Two Integers
- Leetcode: Divide Two Integers
- Divide Two Integers -- LeetCode
- 微信公众帐号开发教程第9篇-QQ表情的发送与接收
- shell调用sqlplus例子
- 微信公众帐号开发教程第10篇-解析接口中的消息创建时间CreateTime
- 并行计算vs分布式计算
- ZOJ-2759
- leetcode: Divide Two Integers
- xmu 1008
- 股市基础知识、股票名词【二】
- Spring声明式事务之代理机制与tx标签两种方式
- 选夫婿1(SDUT 1569)
- 浅入浅出SQL注入
- 微信公众帐号开发教程第11篇-符号表情的发送(上)
- tinyos--随机数
- mysql 库名 表名 大写