• [Leetcode 87] 29 Divide Two Integers


    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
  • 相关阅读:
    网页使用思源字体 CSS
    不用軟體解PPT密碼
    服务端性能测试校准v1.2
    开发语言性能对比,C++、Java、Python、LUA、TCC
    数值溢出测试工具,协议测试专用
    真人算法演示
    Visual Studio并发Qpar优化效果
    服务端性能测试工具校验v1.1
    IOCP和WSA异步协同客户端版
    服务端性能测试工具校验v1.0
  • 原文地址:https://www.cnblogs.com/freeneng/p/3213584.html
Copyright © 2020-2023  润新知