Created
October 29, 2014 05:05
-
-
Save fjc-oai/5fc3337a2f8e5aa4b505 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int divide(int dividend, int divisor) { | |
int flag=1; int ret=0; | |
if(dividend==0)return 0; | |
if(divisor==INT_MIN)return dividend==INT_MIN?1:0; | |
if(divisor<0){ | |
divisor=-divisor; | |
flag*=-1; | |
} | |
if(dividend==INT_MIN){ret=1;dividend+=divisor;} | |
if(dividend<0){ | |
dividend=-dividend; | |
flag*=-1; | |
} | |
while(dividend>=divisor){ | |
int d=divisor,r=1; | |
while(dividend-d>=d){ | |
d+=d; | |
r+=r; | |
} | |
dividend-=d; | |
ret+=r; | |
} | |
return ret*flag; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment