HackerRank "The Great XOR"

本文通过一种离散思考策略对比了HackerRank与LeetCode的问题解决方式,提出了一种观察并逐位分析的方法来解决特定类型的编程挑战。通过一个具体的Python实现案例,展示了如何检查数字的二进制表示,并利用XOR操作来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/tonix/p/6845021.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值