Divide Two Integers
来源:互联网 发布:淘宝3c数码配件 编辑:程序博客网 时间:2024/05/22 18:29
Divide two integers without using multiplication, division and mod operator.
解答:
public int divide(int dividend, int divisor) {boolean negative = (dividend<0 && divisor>0) ||(dividend>0 && divisor<0);int result = 0;long div_dividend = Math.abs((long)dividend);//Math.abs(-2147483648)的结果为-2147483648,这是因为溢出的原因,因此这里一定先要做类型转换才行long div_divisor = Math.abs((long)divisor);while(div_dividend >= div_divisor) {int shift = 0;while( (div_divisor<<shift) <= div_dividend ) {++shift;}--shift;result += (1<<shift);div_dividend -= (div_divisor<<shift);}return negative? -result : result; }
这道题一定要注意整数溢出的问题
0 0
- LeetCode: Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode] Divide Two Integers
- Divide Two Integers
- Divide Two Integers
- LeetCode28:Divide Two Integers
- [Leetcode] Divide Two Integers
- LeetCode Divide Two Integers
- Divide Two Integers
- 【leetcode】Divide Two Integers
- Divide Two Integers
- Divide Two Integers
- [LeetCode]Divide Two Integers
- Divide Two Integers
- LeetCode-Divide Two Integers
- Divide Two Integers
- 25 - Divide Two Integers
- 用户组修改
- 凡提交您的文章,如何长时间才能批准之前
- 低光数码单反相机而夜间拍摄设置
- 冒泡排序_汇编语言的子程序调用与宏定义实现
- 代码分析你的网站从SEO的角度来看
- Divide Two Integers
- Android性能优化之使用线程池
- hdu~1010(dfs)
- 最短路模版整理
- 从Ajax的Web站点中提取数据
- 丹尼尔·克雷格詹姆斯·邦德锻炼培训
- java中的堆和栈
- 个性化的价格每头服务
- VS2010 更改MFC标题栏图标和生成的执行文件图标