share my code with binary search implemented


  • 0
    H

    class Solution {
    private:
    int count = 0;
    int summax(int div1, int div2){
    int divsum = div2;
    count = 1;
    while(divsum <= div1 -divsum ){
    divsum += divsum;
    count = count + count;
    }
    return divsum;
    }
    public:
    int divide(int dividend, int divisor) {
    if(divisor == 0) return NULL;
    bool mintrue = false;
    if (!divisor || (dividend == INT32_MIN && divisor == -1) ) return INT32_MAX;
    if(divisor == INT_MIN) { int div =( dividend == INT_MIN )? 1: 0; return div; }
    if(dividend == INT32_MIN ) { dividend += abs(divisor); mintrue = true;}

        int Isign = 0;
        if(dividend < 0 ) {
            dividend = -dividend;
            Isign = Isign - 1;
        }
        if(divisor < 0) {
            divisor = - divisor;
            Isign = Isign -1;
        }
        
        int c0 = 0;
        int divsum0= divisor;
        int divdiff = dividend;
        
        while (divdiff >= divisor ) {
            int divsum1 = summax(divdiff, divisor);
            c0 += count;
            divdiff = divdiff - divsum1;
        }
        if(Isign == -1) c0 = -c0;
        if(mintrue) {
         
            if(Isign == -1)  
            { c0 -= 1; }
            else 
            {
                c0 += 1;
            }
            }
       return c0;  
    }
    

    };


  • 0
    G

    your answer doesn't meet the constraint, divide operation is not allowed in this question.
    " while(divsum <= div1 / 2) "


  • 0
    H

    @GoCodeZ Thanks, now it has been resolved


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.