2023每日刷题(九十四)
Leetcode—29. 两数相除
叛逆期实现代码
class Solution {
public:int divide(int dividend, int divisor) {if(dividend == INT_MIN && divisor == -1) {return INT_MAX;} return dividend / divisor;}
};
运行结果
倍增算法实现代码
class Solution {
public:int divide(int dividend, int divisor) {if(dividend == INT_MIN && divisor == -1) {return INT_MAX;} if(dividend == INT_MIN && divisor == 1) {return INT_MIN;}long long res = 0;long long a = labs((long long)dividend);long long b = labs((long long)divisor);if(a < b) {return 0;}while(a - b >= 0) {long long x = 0;while(a - (b << 1 << x) >= 0) {x++;}res += 1 << x;a -= b << x;}if((dividend > 0) ^ (divisor > 0)) {res = -res;}return res > INT_MAX ? INT_MAX: res;}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!