Here is a question: https://leetcode.com/problems/divide-two-integers/submissions/
I have used the property of log10
log(a)-log(b)=log(a/b)
and
pow(10,log10(a))=a
The code seems to be wrong for
a= -2147483648
b= -1
expected output: 2147483647
code output: -2147483648
What could be the possible problem in the code?
class Solution {
public:
int divide(int a, int b) {
int neg=1;
if(a>INT_MAX) a=INT_MAX;
if(a<INT_MIN) a=INT_MIN;
if((a>0 && b<0) || (a<0 && b>0)) neg=-1;
if(a<0) a=abs(a);
if(b<0) b=abs(b);
if(b==1) return a*neg;
return neg*(pow(10,log10(a)-log10(b)));
}
};
Aucun commentaire:
Enregistrer un commentaire