Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.
Test instructions: Divide by two numbers without multiplication and mod calculations.
Idea: Because each number can be expressed as a binary, that is, num = a*2^0 + b*2^1 ...., so we just have to determine which 2^k can add results, the first way is the loop, the second is to find the maximum 2^k each time, the problem is to change 2 to B just
Class Solution {public: int divide (int dividend, int divisor) { long long a = ABS ((double) dividend); Long Long B = ABS (double) divisor); Long long res = 0; while (a >= b) { long long c = b; for (int i = 0; a >= C; i++, C <<= 1) { A-= C; Res + = 1<<i; } } int flag = ((divisor ^ dividend) >> 31)? -1:1; if (RES * flag > Int_max) return int_max; else return res * flag;} ;
Class Solution {public: int divide (int dividend, int divisor) { long long a = ABS ((double) dividend); Long Long B = ABS (double) divisor); Long long res = 0; while (a >= b) { long Long T = b, i = 1; while ((T << 1) < a) { T <<= 1; I <<= 1; } A-= t; res + = i; } int flag = ((divisor ^ dividend) >> 31)? -1:1; if (RES * flag > Int_max) return int_max; else return res * flag;} ;
Leetcode Divide, integers