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
  • 相关阅读:
    Unity 粒子系统 特效 移除屏幕外面后再移回来 不会显示问题
    同步读取各平台StreamingAssets文件
    cocos2d-x for android 环境搭建&交叉编译
    lua 热更新
    php连接mysql超时问题
    svn仓库自动同步(主库、从库自动同步)
    游戏开发进度、状况以及结果的关系(个人感言)
    centos 重启服务命令
    编译时,输出信息重定向到文件
    vs开发的程序内存错误
  • 原文地址:https://www.cnblogs.com/freeneng/p/3213584.html
Copyright © 2011-2022 走看看