My simple 3 lines python solution. 192ms, beat 73%


  • 0
    A
     while n>0 and n>m:
            n -= (n&-n)
     return n

  • 0
    B

    can you please explain?, I kind of understand what you are doing here with the two's complement but how it's decreasing in size with n&-n is just purely amazing..


  • 0
    A

    n-(n&-n) just remove the rightest "1" in bits. It is inspired by Binary Index Tree.


Log in to reply
 

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