Problem:
Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.
Solution: Can not be multiplication on the line, but one problem is that the addition and subtraction may be too slow, so need to convert, because any number can be expressed as binary, so there are dividend=divisor* (a*2^1 + b*2^2 + ... + m*2^k)
So just calculate all the divisor*2^k, then subtract.
Topic: Given two integers, requires no multiplication method and modulo operation, the value of a/b is calculated, when the result is out of bounds, the output int maximum value
Java source Code (260MS):
public class Solution {public int divide (int dividend, int divisor) { long a,b,flag=0,sum=0; Long[] map=new long[33],times=new long[33]; if (dividend<0 && divisor<0) flag=1; else if (dividend>0 && divisor>0) flag=1; A=math.abs ((long) dividend); B=math.abs ((long) divisor); int i=0; Map[0]=b;times[0]=1; while (map[i]<=a) { i++; MAP[I]=MAP[I-1]+MAP[I-1]; TIMES[I]=TIMES[I-1]+TIMES[I-1]; } for (int. j=i-1;j>=0;j--) { while (a >= map[j]) { a-=map[j]; SUM+=TIMES[J]; } } Sum=flag==1?sum:-sum; if (Sum>integer.max_value | | sum<integer.min_value) return integer.max_value; return (int) sum; }}
C Language Source code (4MS):
Long Long, ABS (long Long a) { return a>0?a:-a;} int divide (int dividend, int divisor) { int i=0,j,flag=0; Long long sum=0,a,b,map[33],times[33],stop=1; Stop= ((Long Long) 2147483647) +1; if (divisor==0) return int_max; if (dividend==0) return 0; if ((dividend>0 && divisor>0) | | (dividend<0 && divisor<0)) flag=1; A=abs ((Long long) dividend); B=abs ((Long long) divisor); Map[0]=b;times[0]=1; while (Map[i] <= a && i<33) { i++; MAP[I]=MAP[I-1]+MAP[I-1]; TIMES[I]=TIMES[I-1]+TIMES[I-1]; } for (j=i-1;j>=0;j--) { while (a >= map[j]) { a-=map[j]; SUM+=TIMES[J]; } } Sum=flag?sum:-sum; if (sum<int_min | | sum > INT_MAX) return int_max; return (int) sum;}
C + + source code (24MS):
Class Solution {public: int divide (int dividend, int divisor) { int i=0,j,flag=0; Long long sum=0,a,b,map[33],times[33],stop=1; stop=stop<<31; if (divisor==0) return int_max; if ((dividend>0 && divisor>0) | | (dividend<0 && divisor<0)) flag=1; A=abs ((Long long) dividend); B=abs ((Long long) divisor); Map[0]=b;times[0]=1; while (Map[i] <= STOP) { i++; MAP[I]=MAP[I-1]+MAP[I-1]; TIMES[I]=TIMES[I-1]+TIMES[I-1]; } for (j=i-1;j>=0;j--) { while (a >= map[j]) { a-=map[j]; SUM+=TIMES[J]; } } Sum=flag?sum:-sum; if (sum<int_min | | sum > INT_MAX) return int_max; return (int) sum; }};
Python source code (72MS):
Class solution: # @param {integer} dividend # @param {integer} divisor # @return {integer} def divide ( Self, dividend, divisor): flag=0 if dividend>0 and divisor>0:flag=1 elif dividend<0 and divisor <0:flag=1 if Dividend==0:return 0 dividend=abs (dividend) divisor=abs (divisor) map=[0 for I in Range (times=[1)] " i=0 map[0]=divisor;times[0]=1 while map[i]<=dividend : i+=1 map[i]=map[i-1]+map[i-1] times[i]=times[i-1]+times[i-1] j=i-1;sum=0 while j> =0: While map[j]<=dividend: dividend-=map[j] sum+=times[j] j-=1 sum =-sum if flag==0 else sum if Sum>2147483647:return 2147483647 return sum
Leetcode Divide, integers (c,c++,java,python)