Non-bit manipulation solution


  • 0
    D

    Still I haven't lernt the bit operations, so I tried to realize it with iterations. It passed 12/13 tests except of that (a = 2147483647,b = -2147483648,Output: 2147483647, Expected: -1)

    public int getSum(int a, int b) {
            int sum = 0;
            for(int i = 0; i < Math.abs(a); i++) {
                if(a < 0)
                sum--;
                if(a > 0)
                sum++;
            }
            for(int j = 0; j < Math.abs(b); j++) {
                if(b < 0)
                sum--;
                if(b > 0)
                sum++;
            }
        return sum;
    }

  • 0
    D

    So what's the problem with that realization?
    (Except of bad Big-O notation, slow speed and etc.)


  • 0

    Good solution.
    But Time Limit Exceeded....


  • 0
    U

    ++ and -- aren't allowed.


Log in to reply
 

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