Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
题目要求不能使用除号、乘号和取余操作符。
我们使用移位操作符。<< 左移代表乘以2,>>右移代表除以2
//a,b分别为被除数和除数 public int divide(int a,int b){ long res=0; int sign = 1; if((a<0 && b>0)||(a>0&&b<0)){ sign = -1; } if(b==0||(a==Integer.MIN_VALUE&&b==-1)){ return Integer.MAX_VALUE; } if(a b){ long temp = b; long p = 1; while(a>=(temp<<1)){ temp<<=1; p<<=1; } a-=temp; res+=p; } res = sign*res; return (int) res; }