Simple JAVA Solution


  • 0

    ...

    public int divide(int dividend, int divisor) {
        if (divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)) return Integer.MAX_VALUE;
        
        boolean pos = (dividend < 0) ^ (divisor < 0);
        long lDividend = Math.abs((long) dividend);
        long lDivisor = Math.abs((long) divisor);
        
        long[] sorted = new long[32];
        sorted[0] = lDivisor;
        for (int i = 1; i < 32; i++) sorted[i] = sorted[i-1] << 1;
        
        int cnt = 0;
        while (lDividend >= lDivisor) {
            int i = 1;
            while (i < 32 && lDividend >= sorted[i]) i++;
            
            lDividend -= sorted[i-1];
            cnt += (1<<(i-1));
        }
        
        return pos ? -cnt : cnt;
    }
    

    ...


Log in to reply
 

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