标签:font result pad out 边界条件 repeat auto import http
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution { public : int divide( int dividend, int divisor) { if ( !divisor || (dividend == INT_MIN && divisor == -1)) return INT_MAX; int sign = (dividend<0) ^ (divisor<0)? -1: 1; long long a = labs (dividend); long long b = labs (divisor); long long result = 0; while ( a >= b){ for ( long long i = 1, d = b; a >= d; ++i, d <<= 1){ a -= d; result += 1 << (i-1); } } return result*sign; } }; |
标签:font result pad out 边界条件 repeat auto import http
原文地址:http://www.cnblogs.com/zhxshseu/p/0b653e37925689dd2642919cf767d410.html