LeetCode刷题(C++)——Divide Two Integers(Medium)
来源:互联网 发布:傲剑易经升级数据大全 编辑:程序博客网 时间:2024/05/16 03:51
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
class Solution {public: int divide(int dividend, int divisor) { if (divisor == 0 || (dividend == INT_MIN&&divisor == -1))return INT_MAX;bool neg = (dividend < 0) ^ (divisor < 0) ? false : true;long long a = abs((long long)dividend);long long b = abs((long long)divisor);long result = 0;if (b == 1)return neg ? a : -a;while (b <= a){long long x = b;long long shift = 0;while ((x << 1) <= a){x <<= 1;shift += 1;}result += 1<<shift;a = a - x;}return neg ? result : -result; }};
1 0
- LeetCode刷题(C++)——Divide Two Integers(Medium)
- 【leetcode-29】Divide Two Integers(C)
- [LeetCode] 029. Divide Two Integers (Medium) (C++/Python)
- [Leetcode 29, medium] Divide two integers
- LeetCode-29-Divide Two Integers(Numbers)-Medium
- Leetcode 29. Divide Two Integers (Medium) (cpp)
- LeetCode:29.Divide Two Integers[Medium]
- Leetcode刷题记——29. Divide Two Integers(整数相除Divide two integers without using multiplication, division)
- 【LeetCode算法练习(C++)】Divide Two Integers
- LeetCode——Divide Two Integers
- LeetCode——Divide Two Integers
- LeetCode——Divide Two Integers
- leetcode 029 —— Divide Two Integers
- LeetCode题解——Divide Two Integers
- Leetcode 29 —— Divide Two Integers
- Leetcode——29. Divide Two Integers
- Leetcode笔记(7)Divide Two Integers
- LeetCode(29)Divide Two Integers
- POJ 1044 Date bugs 笔记
- IIS发布ASP.NET应用常见错误及解决办法
- 一个真正的全栈工程师
- HDU6025 Coprime Sequence
- 什么是网关及网关作用
- LeetCode刷题(C++)——Divide Two Integers(Medium)
- 深入理解JVM-类加载
- 为什么JDK源码中,无限循环大多使用for(;;)而不是while(true)?
- POJ 1815 Friendship(字典序最小最小割)
- 第一章动态网页开发基础
- 活动调度
- Gym
- RabbitMQ使用理解
- webpack配置