LeetCode 29. Divide Two Integers
来源:互联网 发布:苹果电脑流程图软件 编辑:程序博客网 时间:2024/05/16 23:53
In order to solve this problem, it is good to know some basics first.
Suppose, dividend is a1, divisor is a0.
a1 = a0 * (2^k) + a0 *(2^(k-1)) + a0*(2^(k-2)) + ... a0 *(2 ^ 1)+ a0*(2^0) +a2...(a2 < a0) if it is dividable, the result is k + k-1 +...0
Several examples can better illustrate this problem.
(9, 4) 9 = (8, 4) + (1, 4), (8, 4) == 2, (1, 4) == 0, Thus, (9, 4) = 2
(15, 4) = (8, 4) + (4, 4) + (3, 4) ---> 3
To find the (8, 4), we just need to shift 4 for k left steps, until it is larger than the dividend. k is the result.
#include <iostream>#include <climits>using namespace std;/* Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.*/int divide(int dividend, int divisor) { bool negativeFlag = false; if(dividend < 0 && divisor >= 0) negativeFlag = true; if(dividend >= 0 && divisor < 0) negativeFlag = true; int newDividend = abs(dividend); int newDivisor = abs(divisor); if(newDivisor == 1) return negativeFlag ? -1* newDividend : newDividend; if(newDivisor == 0) return INT_MAX; if(newDivisor > newDividend || newDividend == 0) return 0; int count = 0; while(newDividend > newDivisor) { // first round made an error here. int tmp = newDivisor; int k = 0; while(tmp <= newDividend) { k++; tmp = tmp << 1; } count += 1 << (k-1); // made an error here too. newDividend = newDividend - (tmp >> 1); } return negativeFlag ? -1 * count : count;}int main(void) { cout << "9 divide 3" << endl; cout << "res: " << divide(9, 3) << endl; cout << "9 divide 4" << endl; cout << "res: " << divide(9, 4) << endl; cout << "9 divide -3" << endl; cout << "res: " << divide(9, -3) << endl; cout << "9 divide 10" << endl; cout << "res: " << divide(9, 10) << endl; cout << "9 divide 0" << endl; cout << "res: " << divide(9, 0) << endl; cout << "9 divide 1" << endl; cout << "res: " << divide(9, 1) << endl;}// A recursion version#include <iostream>using namespace std;int divideTwo(long long int divident, long long int divisor) { if(divident == 0 || divisor > divident) return 0; if(divisor == 1) return divident; long long tmp = divisor, k = 0; for(; divident >= tmp; ++k) { if(divident - tmp < divisor) { return 1 << k; } tmp <<= 1; } return divideTwo(divident - (tmp >> 1), divisor) + (1 << (k - 1));}int divideTwo(int divident, int divisor) { long long int d0 = divident; long long int d1 = divisor; bool negative; if((d0 > 0 && d1 > 0) || (d0 < 0 && d1 < 0)) negative = false; else negative = true; int res = divideTwo(d0, d1); return negative ? -res : res;}
0 0
- [LeetCode]29.Divide Two Integers
- LeetCode --- 29. Divide Two Integers
- LeetCode 29.Divide Two Integers
- [Leetcode] 29. Divide Two Integers
- 【leetcode】29. Divide Two Integers
- *LeetCode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- [LeetCode]29. Divide Two Integers
- LeetCode 29. Divide Two Integers
- LeetCode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- LeetCode-29.Divide Two Integers
- leetcode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- [leetcode]29. Divide Two Integers
- leetcode 29. Divide Two Integers
- LeetCode - 29. Divide Two Integers
- js实现网页图片轮换播放
- MVVM编码指引
- Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)
- Copy List with Random Pointer
- 飛飛(三十一)指向学生的指针
- LeetCode 29. Divide Two Integers
- 集成C#测试框架和Selenium对于Web系统实现自动化测试
- POJ 1979:Red and Black(dfs)
- 设计模式之享元模式
- 面试题小结二
- hdu 5678 ztr loves trees (给一颗有根树,树上的每一个节点有一个权值,每次询问某个子树中所有权值的中位数)
- debian下载、编译、使用Cimg的Demo
- 获取statusbar的高度
- 如何判断链表中有无环