My easy Java solution


  • 1
    I
    public class Solution {
        public int divide(int dividend, int divisor) {
            if(divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)) return Integer.MAX_VALUE;
            long dvd = dividend;
            long dvs = divisor;
            boolean sign = (dvd > 0) ^ (dvs > 0);
            dvd = Math.abs(dvd);
            dvs = Math.abs(dvs);
            int multiplier = 1;
            long mdvs = dvs;
            int ans=0;
            while(dvd >= (mdvs << 1)) {
                mdvs <<= 1;
                multiplier <<= 1;
            }
            while(dvd >= dvs) {
                while(mdvs > dvd) {
                    mdvs >>=1;
                    multiplier >>=1;
                }
                dvd -= mdvs;
                ans += multiplier;
            }
            return sign?-ans:ans;
        }
    }
    

Log in to reply
 

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