[Leetcode 87] 29 Divide Two Integers

本文探讨了不使用乘法、除法和取余运算符来实现整数除法的两种方法:一种是利用二分查找算法找到商;另一种是通过位操作,特别是左移操作来逐步逼近答案。两种方法都需要考虑正负号及边界情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem:

Divide two integers without using multiplication, division and mod operator.

 

Analysis:

I think the problem wants us to use binary search to find the answer instead of simply using some arithmetic operations. So the binary search version comes first. When dealing with it, pay attention to the extrame test cases such (1<<31) and (1<<31)-1, these limit case may be wrong if the int type is chosen. A simple way to fix it is to use the long long type and then convert in back to int when returning the result. Also, the positive or negative value should be considered too.

If we really can't use any such operations, then the only tool we have is bit operation. Using << to double the divisor we have, find the max number that is less than or equal to the dividend. Keep record of the number of shifts we have made and substrct the shiftted number from the dividend, and repeat this process until the dividend is 1 or 0. Sum all these number of shift up and we can also get the answer. There are so many details to care about in this version.

Code:

Binary Search Version:

 1 class Solution {
 2 public:
 3     int divide(int dividend, int divisor) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         long long dd = dividend, di = divisor;
 7         
 8         bool isNeg = false;
 9         
10         if (dd < 0) {
11             dd = -dd;
12             isNeg = !isNeg;
13         }
14         
15         if (di < 0) {
16             di = -di;
17             isNeg = !isNeg;
18         }
19         
20         long long s = 0, e = dd;
21         while (s <= e) {
22             long long mid = (s + e) / 2, mul = mid * di;
23             
24             if (mul <= dd && (mid+1)*di > dd)
25                 return (int) (isNeg ? -mid : mid);
26             else if (mul > dd)
27                 e = mid - 1;
28             else
29                 s = mid + 1;
30         }
31         
32         return 0; //not valid
33     }
34 };
View Code

 

Binary Operation Version:

 1 class Solution {
 2 public:
 3 int divide(int dividend, int divisor) {
 4     // Start typing your C/C++ solution below
 5     // DO NOT write int main() function
 6 
 7 
 8     long long dd = dividend, di = divisor;
 9 
10     bool isNeg = false;
11 
12     if (dd < 0) { dd = -dd; isNeg = !isNeg; }
13     if (di < 0) { di = -di; isNeg = !isNeg; }
14 
15     long long res = 0;
16     while (true) {
17         int sft;
18         for (sft=0; (di<<sft)<dd; sft++)
19             ;
20 
21         if ((di<<sft) == dd)
22             return isNeg ? -((1<<sft) + res) : ((1<<sft) + res);
23         else if (di > dd)
24             return isNeg ? -res : res;
25         else{
26             dd -= (di<<(sft-1));
27             res += (1<<(sft-1));
28         }
29     }
30 
31     return res;
32 }
33 };
View Code

 

 

转载于:https://www.cnblogs.com/freeneng/p/3213584.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值