C++ 8ms solution


  • 1
    X
    class Solution {
    public:
    	int divide(int dividend, int divisor) {
    		if (divisor == 0 || (dividend == INT_MIN && divisor == -1)) {
    			return INT_MAX;
    		}
    		int sign = (dividend ^ divisor) >= 0 ? 1 : -1;
    		long long newdividend = labs(dividend);
    		long long newdivisor = labs(divisor);
    		int ans = 0;
    		while (newdividend >= newdivisor) {
    			long long tmp = newdivisor;
    			int m = 1;
    			while (newdividend >= (tmp << 1)) {
    				m = m << 1;
    				tmp = tmp << 1;
    			}
    			newdividend -= tmp;
    			ans += m;
    		}
    		return ans * sign;
    	}
    };

Log in to reply
 

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