Compiler shows Time Limit Exceeded for Last executed input: 1 (00000000000000000000000000000001)?


  • 0
    U

    import java.math.BigInteger;
    public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(Integer m) {
    BigInteger n = BigInteger.valueOf(m);
    int count=0;
    while(!(n == BigInteger.ZERO)){
    BigInteger k = n.and(BigInteger.valueOf(1));
    if(k.equals(BigInteger.valueOf(1))){
    count++;
    }
    n.shiftLeft(1);
    }
    return count;

    }
    

    }


  • 0
    D

    Operations that are O(1) with primitives are O(n) when using BigIntegers


Log in to reply
 

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