zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    StringHelper类的代码也写得不错,值得好好学习学习
    开发感慨
    取地址参数的方法
    在C#中运用SharpZipLib和unrar进行解压缩
    我的模板分析引擎类PHP的.net开发方法功能介绍篇
    分析模板的一段简单快速的算法片段
    关于文件操作的一些感悟
    关于自定义模板的设计
    JS中的函数、Bom、DOM及JS事件 pixel
    什么是CSS盒模型及利用CSS对HTML元素进行定位的实现(含h5/css3新增属性) pixel
  • 原文地址:https://www.cnblogs.com/freeneng/p/3213584.html
Copyright © 2011-2022 走看看