`
king_tt
  • 浏览: 2248341 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【leetcode】Divide Two Integers

 
阅读更多

Question :

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


Anwser 1 :

class Solution {
public:
    int divide(int dividend, int divisor) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int ret = 0;
        
        if(dividend == 0 || divisor == 0) return 0;
        
        int sign = 1;   // 1 : positive; -1 : negative
        if(dividend < 0) sign *= -1;
        if(divisor < 0) sign *= -1;
        
        long long tmpDiv = dividend;
        long long divL = abs(tmpDiv);
        long long tmpDivisor = divisor;
        long long divisorL = abs(tmpDivisor);
        
        while(divL >= divisorL){
            int count = 1;              // first: divL > divisorL 
            long long sum = divisorL;   // long long, cal divisorL
            while(sum + sum <= divL){
                count += count;
                sum += sum;
            }
            divL -= sum;
            ret += count;
        }
        
        return sign * ret;
    }
};
注意点:

1) 原理:累加除数,判断是否大于被除数

2) 设置符号位sign,判断符号

3) 对dividend和divisor都先转化成long long类型,防止负数转化成正整数时溢出

4) 除数之和sum,必须设置成long long,防止溢出(同2)


Anwser 2 :

class Solution {
public:
    int divide(int dividend, int divisor) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        long long a = dividend;
        long long b = divisor;
        
        int sign = 1;
        if(a < 0){
            a = -a;
            sign *= -1;
        }
        
        if(b < 0){
            b = -b;
            sign *= -1;
        }
        
        int d = 0;
        while ( (b << d) <= a )
        {
            ++d;
        }
        
        --d;
        
        int res = 0;
        for (int i = d; i >= 0; --i) {
            if ( (b << i) <= a ) {
                res += (1 << i);    // high to low
                a -= (b << i);      // remaider
            }
        }

        return sign * res;
    }
};


Anwser 3:

class Solution {
 private:
     long long f[100];
 public:
     int bsearch(long long a[], int left, int right, long long key)
     {
         if (left > right)
             return -1;
             
         int mid = left + (right - left) / 2;
         if (a[mid] == key)
             return mid;
         else if (a[mid] < key)
         {
             int pos = bsearch(a, mid + 1, right, key);
             return pos == -1 ? mid : pos;
         }
         else
         {
             return bsearch(a, left, mid - 1, key);
         }
     }
     
     int divide(int dividend, int divisor) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         int sign = dividend < 0 ? -1 : 1;
         if (divisor < 0)
             sign *= -1;
         
         long long div = dividend;
         div = abs(div);
         long long divisorL = divisor;
         divisorL = abs(divisorL);
         f[0] = divisorL;
         int size = 1;
         while(true)
         {
             if (f[size-1] >= div)
                 break;
             f[size] = f[size-1] + f[size-1];
             size++;
         }
         
         int num = 0;
         long long sum = 0;
         while(div > 0)
         {
             int pos = bsearch(f, 0, size - 1, div);
             if (pos == -1)
                 break;
             div -= f[pos];
             num += (1 << pos);
         }
                 
         return num * sign;
     }
 };


分享到:
评论

相关推荐

    c语言-leetcode 0029-divide-two-integers.zip

    c c语言_leetcode 0029_divide_two_integers.zip

    js-leetcode题解之29-divide-two-integers.js

    js js_leetcode题解之29-divide-two-integers.js

    leetcode叫数-python01:Python01

    先来看LeetCode-29上的Divide Two Integers题目要求: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 1 2 3 就是说不用乘法,除法,求模运算...

    LeetCode最全代码

    371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...

    leetcode1-240题中文题解,md格式,java

    8. leetcode-29-Divide-Two-Integers.md:第29题,两数相除,涉及到整数除法和位运算。 9. leetcode-218-The-Skyline-Problem.md:第218题,天际线问题,可能涉及到二维数组处理和线段树或平衡二叉搜索树的数据结构...

    _leetcode-python.pdf

    - Divide Two Integers: 实现整数除法,不能使用乘法、除法和模运算符。 - Search in Rotated Sorted Array: 在旋转过的排序数组中进行二分查找。 - Search for a Range: 给定一个按照升序排列的数组,和一个目标值...

    leetcode切割分组-leetcode:leetcode

    029_divide_two_integers*.py # 实现整除 050_pow.py # 实现乘幂 066_plus_one.py # 数列末尾值+1 069_sqrt.py # 实现开根号 136_single_number.py # 位操作:异或(xor)操作 x ^ 0 = x; x ^ x = 0 sum 001_two_sum....

    丢失的最小正整数leetcode-interview-algorithm:面试时的JS算法

    Divide Two Integers 两个整数相除,要求不能使用 *,/,以及mod操作符,返回除数,若除数有小数点,只返回整数部分,如2.789,只应返回2,此题为leetcode上的题目 Random Numbers 用计算机生成了N个1到1000之间的...

    lovely-nuts:这是一个可爱的坚果

    Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档....29.Divide Two Integers 二进制累加代替除法,防止溢出 36.Valid Sudoku set去重复 2018/04/19: 038.Count and Say 递归 040.C

    Dir-For-LeetCode

    问题 完全的 017_Letter_Combinations_of_a_Phone_Number 018_4总和 019_Remove_Nth_Node_From_End_of_List 020_Valid_Parentheses ... 029_Divide_Two_Integers 030_Substring_with_Concatenation_of

    leetcodepalindrom-solutions_to_problems:解决问题的解决方案

    5. **除以两个整数(Divide Two Integers)** 任务是实现一个函数,模拟整数除法,返回商(不考虑浮点数部分)。这个题目需要考虑整数溢出问题,可以使用位运算和减法进行求解。例如,将除数和被除数都乘以一个足够大...

Global site tag (gtag.js) - Google Analytics