Divide Two Integers
来源:互联网 发布:wav音轨分离软件 编辑:程序博客网 时间:2024/06/04 19:53
Divide two integers without using multiplication, division and mod operator.
思路:不能用乘除模运算,只能用减法或加法做了
代码一:Time limit exceed
public static int divide(int dividend, int divisor) { int result = 0; if(dividend < 0 && divisor < 0) return divide(Math.abs(dividend), Math.abs(divisor)); else if(dividend < 0 && divisor > 0) return -1 * divide(Math.abs(dividend), divisor); else if(dividend > 0 && divisor < 0) return -1 * divide(dividend, Math.abs(divisor)); else { while(dividend >= divisor) { result++; dividend -= divisor; } return result; } }
代码二:类似二分的思想
public static int divide_2(int dividend, int divisor) {if(dividend == 0 || divisor == 0)return 0;int flag = 1, tmp, result = 0;if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0))flag = -1;long c = dividend;long d = divisor;long a = Math.abs(c);long b = Math.abs(d);if(b > a)return 0;while(a >= b){tmp = 1;long sum = b;while(sum + sum <= a){sum += sum;tmp += tmp;}a -= sum;result += tmp;}return flag * 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
- 节能灯多少钱,价格贵吗
- 阿里巴巴校招笔试题整理
- js 在页面加入人体时钟
- JBOSS连接池4-连接获取及返还和销毁
- JAVA异常处理机制
- Divide Two Integers
- 重新开搞 devexpress VCL
- ios判断邮箱,手机号码,车牌号是否合法(正则表达)
- 数字签名是什么?
- Android编程实战--安全卫士(2)
- Ice的更多细节
- Activity的运行状态简要介绍
- GIF动画附加码的例子
- 7种网络编程I/O模型代码实现实例