C++, Not sure if this is any good but 0ms anyway

  • 0

    So basically using a+b=log(e^a * e^b), however, with a little trick to avoid inf and nan

    class Solution {
        int getSum(int a, int b) {
            double A = (double)a;
            double B = (double)b;
            return (int)(round(B*log(exp(A/B)*exp(1))));

  • 0

    nvm, this is wrong despite passing test cases. it breaks when b=0, ugh
    I guess it needs a if(b==0){return a;}

  • 0

    Thanks! But why "(B*log(exp(A/B)*exp(1)))" ?
    I think "log( ( exp(A)*exp(B) ) )" is more readable.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.