Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Hey I found problems on HackerRank is more interesting than ones on LeetCode..

Strategy: observe\analyze bit by bit - DISCRETE THINKING. The idea is like this: if we can XOR any bit in original number X to make that bit into 1 from 0, then any bits following that bit don't matter; and we go check bit by bit. This is also the sln in editorial.

t = int(raw_input())

for i in xrange(t):
    n = int(raw_input())
    j = 0
    cnt = 0
    
    while n > 0:
        if (n&1) == 0:
            cnt += pow(2, j)
        j += 1
        n >>= 1
    print cnt
View Code
posted on 2017-05-12 12:43  Tonix  阅读(162)  评论(0)    收藏  举报