[LeetCode] Divide Two Integers
来源:互联网 发布:ubuntu怎么稳定翻墙 编辑:程序博客网 时间:2024/05/16 11:39
Divide two integers without using multiplication, division and mod operator.
这道题很有意思!
什么是除,除的结果是什么?
6 / 2 = 3 其实代表了6 = 2 + 2 + 2,即3个2相加,所以除其实是加的合并!
这里需要注意边界范围,long long
class Solution {public: int divide(int dividend, int divisor) { int res = 0; bool flag = false; if ( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ) { flag = true; } long long a = abs((long long)(dividend) ); long long b = abs((long long)(divisor) ); if (a < b) return 0; while (a >= b) { long long temp = b; int count = 1; while (temp + temp < a) //everytime double it to make the process fast! { temp += temp; count += count; } //if the process make the divisor over the divident, miuse it a = a - temp; res += count; } if (flag == true) return -res; else return 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
- POJ 3112 Digital Biochemist Circuit
- Fiddler Web Debugger查看Http报文信息使用说明
- Java Runtime.exec 调用linux SCP 命令Copy 远程机器上的目录失败
- 通用线程:POSIX 线程详解,第 2部分
- 通用线程:POSIX 线程详解,第 3 部分
- [LeetCode] Divide Two Integers
- hdoj 2674 N!Again 【同余定理】+【找规律】
- C语言计算浮点数的小数位数,屏蔽掉了浮点运算的误差
- js数组的操作
- BING objectness 代码运行 vs2012 64位
- HDU 1087 && POJ 2533(DP,最长上升子序列).
- php学习路线
- 【IIS】windows2003 iis6.0站点打不开,找不到服务器或 DNS 错误
- Pager-taglib分页使用