leetcode Divide Two Integers

题目链接

public class Solution {
    public int divide(int dividend, int divisor) {
long mydivend=dividend;
        long mydivsor=divisor;


        boolean posDividend=true;
        boolean posDivisor=true;
        if(mydivend<0l)
        {
            posDividend=false;
            mydivend=0l-mydivend;
        }
        if(mydivsor<0l)
        {
            posDivisor=false;
            mydivsor=0l-mydivsor;
        }

        long result=0;
        long myDivisor=mydivsor;
        int movieCount=0;
        while((myDivisor<<1)<=mydivend)
        {
            myDivisor=myDivisor<<1;
            movieCount++;
        }



        while(movieCount>-1)
        {
            Long current=0l;
            while(mydivend-myDivisor>=0)
            {
                current++;
                mydivend-=myDivisor;
            }

            result+=(current<<movieCount);

            movieCount--;
            myDivisor=myDivisor>>1;
        }
        if(posDividend^posDivisor)
        {
            if(result>Integer.MAX_VALUE)
            {
                return Integer.MIN_VALUE;
            }
            return (int)(0L-result);
        }
        else
        {
            if(result>Integer.MAX_VALUE)
            {
                return Integer.MAX_VALUE;
            }
            return (int)result;
        }
    }
}

你可能感兴趣的:(leetcode Divide Two Integers)